我有一个功能。如何检查T对象的类型?
protected Boolean MainFunction<T>(T objectData, string Id, out string Value)
{
//here I need to check whether T equal the object I need or not
}
和上面MainFunction
由Function1
和Function2
其中I通过MyObject1
和MyObject2
调用。
我想检查T objectData
无论是MyObject1
还是MyObject2
里面的MainFunction
。请给我任何建议。
public bool Function1(string ID, out string Value, out ErrorReport error)
{
return MainFunction(Data.MyObject1, ID, out Value, out error);
}
public bool Function2(string ID, out string Value, out ErrorReport error)
{
return MainFunction(Data.MyObject2, ID, out Value, out error);
}
你能举个例子说明为什么你需要知道传入的类型吗? – 2014-10-06 14:27:39
目前还不清楚你的意思是什么:''我想检查T objectData是否是MainObject中的MyObject1或MyObject2“ - 你想确定什么? – David 2014-10-06 14:28:00
拥有“MyObject1”和“MyObject2”的基类,并使用[where T:BaseClass'] [通用约束](http://msdn.microsoft.com/zh-cn/library/d5x73970.aspx)您可能会重新考虑您的设计,如果您使用泛型方法检查类型,那么您可能不需要泛型。 – Habib 2014-10-06 14:28:09