小编Kar*_*ahr的帖子

是否有通用类使用静态方法的解决方法?

我有一个相当简单的问题,但在C#中似乎没有解决方案.

我有大约100个Foo类,每个类都实现一个static FromBytes()方法.还有一些泛型类应该使用这些方法FromBytes().但是泛型类不能使用这些static FromBytes()方法,因为它们T.FromBytes(...)是非法的.

我是否遗漏了某些内容或者无法实现此功能?

public class Foo1
{
    public static Foo1 FromBytes(byte[] bytes, ref int index)
    {
        // build Foo1 instance
        return new Foo1()
        {
            Property1 = bytes[index++],
            Property2 = bytes[index++],
            // [...]
            Property10 = bytes[index++]
        };
    }

    public int Property1 { get; set; }
    public int Property2 { get; set; }
    // [...]
    public int Property10 { get; set; }
}

//public class Foo2 { ... }
// …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

8
推荐指数
2
解决办法
476
查看次数

标签 统计

c# ×1

generics ×1

reflection ×1