【问题标题】:Elegant way to connect an enum value to a method group将枚举值连接到方法组的优雅方式
【发布时间】:2014-07-20 12:15:41
【问题描述】:

我正在创建一个网络应用程序。 为此,我正在创建一个消息传递系统。 到目前为止我所拥有的(至少是一个 sn-p...)

public static byte[] ClassToBytes(GenericMessage Data)
{
    int Size = Marshal.SizeOf(Data);
    byte[] Buffer = new byte[Size];
    IntPtr Ptr = Marshal.AllocHGlobal(Size);

    Marshal.StructureToPtr(Data, Ptr, true);
    Marshal.Copy(Ptr, Buffer, 0, Size);
    Marshal.FreeHGlobal(Ptr);

    return Buffer;
}

public static GenericMessage BytesToClass(byte[] Source, ClassType Type)
{
    switch (Type) //This is going to be extended
    {
        case ClassType.GenericMessage:
            return BytesToClass<GenericMessage>(Source);
            break;
        case ClassType.RequestMessage: //RequestMessage is derived from GenericMessage
            return BytesToClass<RequestMessage>(Source);
            break;
        case ClassType.ResponseMessage: //ResponseMessage is derived from GenericMessage
            return BytesToClass<ResponseMessage>(Source);
            break;
        default:
            throw new KeyNotFoundException();
            break;
    }
}

public static T BytesToClass<T>(byte[] Source)
{
    int Size = Marshal.SizeOf(typeof(T));
    IntPtr Ptr = Marshal.AllocHGlobal(Size);
    Marshal.Copy(Source, 0, Ptr, Size);
    T result = (T)Marshal.PtrToStructure(Ptr, typeof(T));
    Marshal.FreeHGlobal(Ptr);
    return result;
}

基本上我想做的是:

public static GenericMessage BytesToClass(byte[] Source, ClassType Type)
{
    return BytesToClass<Type>(Source);  
}

枚举或者字典有没有这样的方法?

我已经尝试并搜索过,但没有任何结果。

【问题讨论】:

  • 您可能缺少的搜索词是“c# generic method makegenericmethod”。
  • @usr,是什么让您认为这个问题只是关于尝试使用反射调用泛型方法?我认为你错了。他正在尝试将枚举与 Type 相关联,这完全不同。
  • @KirkWoll 下面“基本上我想做的是:”他说他想动态提供一个泛型类型参数。这似乎是他缺少的部分。我会保持打开状态。
  • @KirkWoll 看看已经到来的答案。就像我今天关闭的约 20 个副本中的最佳答案一样,所有这些都是关于 MakeGenericMethod 的。微小的变化。这里的变化是他没有类型,而是一个作为类型名称的字符串。
  • @usr,问题是如何获取类型参数,荷兰人地址,但其他所谓的重复没有。

标签: c# enums .net-3.0


【解决方案1】:

如果您想动态提供泛型类型,正如 @usr 在 cmets 中所评论的那样,您可以这样做:

    public static GenericMessage BytesToClass(byte[] Source, ClassType MyType)
    {
        // Gets the Type we want to convert the Source byte array to from the Enum 
        Type _targetType = typeof(Program).GetNestedType(MyType.ToString());
        // Gets the generic convertion method, note we have to give it the exact parameters as there are 2 methods with the same name
        var _convMethod = typeof(Program).GetMethod("BytesToClass", new Type[] { typeof(byte[]) });
        // Invoke the generic method, setting T as _targetType
        var result = _convMethod.MakeGenericMethod(new Type[] { _targetType }).Invoke(null, new object[] { Source });
        return (GenericMessage)result;
    }

ClassType enum 成员的名称必须与它们所代表的类名称完全相同。 此外,此方法假定您要将字节数组转换为的类位于Program 类中(请参阅typeof(Program))。显然,您应该更改它以使其适用于您的程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 2011-12-09
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多