views:

66

answers:

1

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.

+2  A: 

One trick is to use object in the middle to force it:

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

or use as:

RFIDeas_Wrapper wrapper = Reader as RFIDeas_Wrapper;
if (wrapper != null)
{
    wrapper.OpenDevice();
    ...
}
Marc Gravell
@Marc: Then Will it be converting Reader into object type. Will I have any benefit of using T type. I was trying to avoid type conversion. Is it something the same thing or will it be converting it into object type first then its appropriate class type. ?
Shantanu Gupta
@Shantanu Gupta : We aren't converting the object; we are simply casting the reference. As others have noted, it would work the same without the generics. The problem, though, is that by doing this you probably introduce boxing *anyway* (if you have value-type T)
Marc Gravell
@Marc: When should i use T type in these type of cases. When I may get value type as well as reference type ?
Shantanu Gupta
@Shantanu - I'd use `T` when it *adds* something. It doesn't really add anything in your case; just use `object`.
Marc Gravell
@Marc: adds something means, I didn't get you exactly. I m now using object rather than T, but still would like to know reason.
Shantanu Gupta
@Shantanu: T is usually good when using with lists - you want to insert objects of T and return objects of T but you don't care about what T actually is (so you don't need to call methods of the objects): List<T> allow you to add objects of T and return object of T allowing you to use List<int>.Add(int), but the list itself does not use the methods of T because it does not need to care.
František Žiačik
@Frantisek: I understood 100% of what you was trying to deliver. I appreciate for your effort. I was using T till now for making my code reusable rather than what you explained. As We are not using Generic types, Collections at all so I wanted to use them but never knew the purpose. Thanks anyway I would now try to use them in actual direction where they should be used.
Shantanu Gupta