Pure.Krome
Pure.Krome

Reputation: 86937

Having trouble with .NET Generics

I have the following properties in a class...

public IObjectSet<Foo> Foos { get; set; }
public IObjectSet<Baa> Baas { get; set; }
public IObjectSet<PewPew> Pew^2 { get; set; }

I wish to return the value of one of those properties, based upon how a pre-determined generic type.

eg.

public IObjectSet<T> Set<T>() where T : class
{
    //  THIS CODE DOESN'T COMPILE.
    if (T is User)
    {
        return Users as IObjectSet<T>;

    }
    else if (T is Clan)
    {
        return Clans as IObjectSet<T>;
    }
}

So when i have a particular type .. i need to retrieve the correct type data.

eg.

return TheClass.Set<Foo>().AsQueryable();

Is this possible?

Upvotes: 1

Views: 102

Answers (3)

Richard Friend
Richard Friend

Reputation: 16018

Is is used when you have an instance, you need to use

if (typeof(T) == typeof(User)) ..

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174279

Use this:

if(typeof(T) == typeof(User))

Upvotes: 1

manji
manji

Reputation: 47968

if (typeof(T) == typeof(User))

Upvotes: 6

Related Questions