Converting Generic Type into reference type after checking its type using GetType(). How ?
Posted
by Shantanu Gupta
on Stack Overflow
See other posts from Stack Overflow
or by Shantanu Gupta
Published on 2010-06-16T09:23:47Z
Indexed on
2010/06/16
9:32 UTC
Read the original article
Hit count: 293
i am trying to call a function that is defined in a class RFIDeas_Wrapper
(dll being used).
But when i checked for type of reader and after that i used it to call function it shows me error Cannot convert type T to RFIDeas_Wrapper.
EDIT
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)Reader).OpenDevice(); , ((AlienReader)Reader).TagCollection; I want this line to be executed without any issue. As Reader will always be of the type i m specifying. How to make compiler understand the same thing.
© Stack Overflow or respective owner