遇到.net泛型的麻烦



我在一个类中有以下属性…

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

我希望根据预先确定的泛型类型返回其中一个属性的值。

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>;
    }
}

所以当我有一个特定的类型…我需要检索正确的类型数据。

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

这可能吗?

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

Is在您有实例时使用,您需要使用

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

使用

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

最新更新