Converting a Generic Type to a reference type after checking its type with GetType (). How?

I am trying to call a function that is defined in a class RFIDeas_Wrapper(used by the dll). But when I checked the reader type, and after that I used it to call the function, it shows me an errorCannot 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 run without any problems. Since Reader will always indicate type i m. How to make the compiler understand the same thing.

+3
source share
1 answer

, object , :

if (Reader is RFIDeas_Wrapper)
{
    ((RFIDeas_Wrapper)(object)Reader).OpenDevice(); 
    ...
}

as:

RFIDeas_Wrapper wrapper = Reader as RFIDeas_Wrapper;
if (wrapper != null)
{
    wrapper.OpenDevice();
    ...
}
+3

Source: https://habr.com/ru/post/1750252/


All Articles