Given Expression is Never of the Provided Type

When using generics, and you get an error "Given Expression is Never of <X> Type", it is usually how it is assigned.  For example, I was trying to do this:

public IInterface<T> CreateProvider<T>()
{
    if (typeof(T) is ObjectA)
       return new ObjectAProvider() as IInterface<T>;
    .
    .
    .
    else
       throw new NotImplementedException();

This was a problem; I got the above error.  When digging for an answer, I found this as the solution:  http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=111314&SiteID=1   Type has an IsAssignableFrom method, which I was able to do successfully:

if (typeof(T).IsAssignableFrom(typeof(ObjectA)))
    return new ObjectAProvider() as IInterface<T>;
.
.
.
else
    throw new NotImplementedException();

 

Comments

# re: Given Expression is Never of the Provided Type

Friday, March 02, 2012 5:03 AM by handgesdurt

Dear friend, we are a novice in electronic business,  

we've lots of location of study,  

in this post about this web site for primary he brought us a lot of surprise, so we decided to have the activities we've our personal entity shop,  

for the very first time in online opened mall,  

we from the most effective design items.    

We shop in maintain make activities, [url=http://www.armaniwatchea.com]armani [/url]

in the event you adore,    

it will join us right here.    

For those who have any Ideas will carry out,  

we accept modestly. We might, in many techniques usually are not contemplating to, if you have a suggestion to your stand. We will be within the thank you, within the VIP of the discount card. We expect you to join real

# re: Given Expression is Never of the Provided Type

Wednesday, March 21, 2012 11:33 PM by AliblyGaluI

Развлечения тоже должны быть нравственными?  [url=http://xn--c1aeb8eua.xn--p1ai/]чем заняться в свободное время[/url][url=http://xn--c1aeb8eua.xn--p1ai/]чем заняться вечером[/url]

Почему мне в юморе становится грустно от увиденных там"смехохахыхоечков" и "юмористов",невыдержал и нафлудил в страницу?         [url=http://xn--c1aeb8eua.xn--p1ai/]эро видео[/url][url=http://xn--c1aeb8eua.xn--p1ai/]эро приколы[/url]