从泛型类型调用静态方法

时间:2015-01-11 15:45:59

标签: c# generics methods static

我有一个带有泛型参数的静态类,它声明如下:

public static partial class CMSLib<TUser> where TUser : CMSLib<TUser>.UserBase
{
    public abstract class UserBase : OrmObject<TUser>
    {
        public UInt32 Id { get; set; }
        public string Login { get; set; }
        /* etc. */
    }
}

OrmObject声明如下:

public abstract class OrmObject<T> where T: class
{
    static OrmObject()
    {
        using (var db = Db.Open())
            db.CreateTable<T>();
    }

    public static T Single(Expression<Func<T, bool>> where) { ... }
}

当我从通用参数尝试调用方法时:

public static partial class CMSLib<TUser> where TUser : CMSLib<TUser>.UserBase
{
    public static TUser SomeFunction()
    {
        var user = TUser.Single(...); // Here error
        ...
    }
}

我收到错误&#39; TUser&#39;是一个&#39;类型参数&#39;,在给定的上下文中无效

在这种情况下我如何调用 TUser.Single 方法(必须调用OrmObject中的静态构造函数)?

1 个答案:

答案 0 :(得分:1)

只需拨打OrmObject<TUser>.Single()而不是TUser.Single()