user3550073
user3550073

Reputation: 33

Call static method from generic type

I has static class with generic argument which declared like this:

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 declared like this:

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

When i trying call method Single from generic parameter:

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

I got error 'TUser' is a 'type parameter', which is not valid in the given context

How in this case i can call TUser.Single method (static constructor in OrmObject must be called)?

Upvotes: 2

Views: 1426

Answers (1)

mrahhal
mrahhal

Reputation: 3497

Just call OrmObject<TUser>.Single() instead of TUser.Single().

Upvotes: 1

Related Questions