在 C# 中传递命名空间
本文关键字:命名空间 | 更新日期: 2023-09-27 18:31:01
我们有一个关于 c# 中继承的练习。现在我的问题是,我将在问号和 if 语句中放入什么,以了解程序是否传递了 Person 类或 Animal 类或 InventoryApplication 命名空间下的任何类。:)
private void AddButton_Click(object sender, EventArgs e)
{
Logic_Layer.Logic logic = new Logic();
//logic.Add<Person>();
}
namespace Logic_Layer
{
public class Logic
{
public void Add<InventoryApplication>() where InventoryApplication : ?
{
//if { }
}
public void delete() { }
public void edit() { }
public void search() { }
public void searchAll() { }
}
}
不能在约束中使用此类语句。但是,稍后在该方法中,您可以执行此操作:
if (typeof(myObject).Namespace == "InventoryApplication")
{
...
}
更好的是,如果您要测试的类(动物,人等)将实现一个接口(例如,IMyInterface
)。
例如:
void Add<T>(<T> param) where T : IMyInterface {/*...*/}