我有带有通用参数的静态类,它声明如下:

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) { ... }
}


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

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


我收到错误“ TUser”是“类型参数”,在给定的上下文中无效

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

最佳答案

只需调用OrmObject<TUser>.Single()而不是TUser.Single()

10-08 19:28