我正在尝试调用类RFIDeas_Wrapper
(正在使用dll)中定义的函数。 但是,当我检查类型读者的之后,我用它来调用函数它让我错误Cannot convert type T to RFIDeas_Wrapper.
使用GetType()检查其类型后,将通用类型转换为引用类型。怎么样?
编辑
private List<string> GetTagCollection<T>(T Reader)
{
TagCollection = new List<string>();
if (Reader.GetType() == typeof(RFIDeas_Wrapper))
{
((RFIDeas_Wrapper)Reader).OpenDevice();
// here Reader is of type RFIDeas_Wrapper
//, but i m not able to convert Reader into its datatype.
string Tag_Id = ((RFIDeas_Wrapper)Reader).TagID();
//Adds Valid Tag Ids into the collection
if(Tag_Id!="0")
TagCollection.Add(Tag_Id);
}
else if (Reader.GetType() == typeof(AlienReader))
TagCollection = ((AlienReader)Reader).TagCollection;
return TagCollection;
}
((RFIDeas_Wrapper)阅读器).OpenDevice();
((AlienReader)Reader).TagCollection;
我想这条线没有任何问题执行。由于读者将永远是我指定的类型。 如何让编译器理解同样的事情。
为什么你使用'T'(可以是任何东西),如果您需要'RFIDeas_Wrapper'? – Bobby 2010-06-16 09:26:30
@Bobby:我可以找到任何一种阅读器。即AlienReader在其他部分如此等等。我必须使这个函数适用于所有返回List Collection的读者。我认为直接传递类对象并获得结果时应该是一种更好的方法,而不必一次又一次地写相同的代码来获得某些输出。 – 2010-06-16 09:27:55
我看不出在这里使用泛型的一点,因为你没有得到任何编译时类型的安全。它也可能是对象。 – 2010-06-16 09:33:59