使用System.Type调用泛型方法 [英] Using System.Type to call a generic method

查看:108
本文介绍了使用System.Type调用泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#/。NET 4.0和提供以下功能的协议缓冲区库(protobuf-net)。

I am using C#/.NET 4.0 and a Protocol Buffers library (protobuf-net) which provides the following functionality.

public static class Serializer {
    public static void Serialize<T>(Stream destination, T instance);
    public static void Serialize<T>(SerializationInfo info, T instance);
    public static void Serialize<T>(XmlWriter writer, T instance);
    public static void Serialize<T>(SerializationInfo info, StreamingContext context, T instance);
    public static T Deserialize<T>(Stream source);
}

我需要用非通用等效项包装其中两个调用。具体来说,我想要

void SerializeReflection(Stream destination, object instance);
object DeserializeReflection(Stream source, Type type);

简单地调用 Serializer 在运行时。 我已经使用 DeserializeReflection 方法来使用以下代码:

that simply call the respective generic members of Serializer at runtime. I have gotten the DeserializeReflection method to work with the following code:

public static object DeserializeReflection(Stream stream, Type type)
{
    return typeof(Serializer)
        .GetMethod("Deserialize")
        .MakeGenericMethod(type)
        .Invoke(null, new object[] { stream });
}

SerializeReflection 方法是什么使我麻烦。我最初尝试了以下代码:

The SerializeReflection method is what is causing me trouble. I at first tried the following code:

public static void SerializeReflection(Stream stream, object instance)
{
    typeof(Serializer)
        .GetMethod("Serialize")
        .MakeGenericMethod(instance.GetType())
        .Invoke(null, new object[] { stream, instance });
}

问题在于 typeof(Serializer )。Invoke(...)不起作用。对 GetMethod( Serialize)的调用使我得到 AmbiguousMatchException ,因为有四个名为 序列化

The problem is that the part between typeof(Serializer) and .Invoke(...) is not working. The call to GetMethod("Serialize") gets me an AmbiguousMatchException, because there are four methods named "Serialize."

然后我尝试使用 GetMethod 的重载接受一个 System.Type 数组来解析绑定:

I then tried using the overload of GetMethod that takes an array of System.Type to resolve the binding:

GetMethod("Serialize", new[] { typeof(Stream), instance.GetType() })

但这只是 GetMethod null 的结果。

如何使用反射来获取 void Serializer.Serialize< T>(Stream,T) MethodInfo c>,其中 T instance.GetType()

How can I use reflection to get the MethodInfo for void Serializer.Serialize<T>(Stream, T), where T is instance.GetType()?

推荐答案

尝试使用下一个代码片段查看它是否满足您的需求。它创建方法 public static void Serialize< T>(流目标,T实例)的封闭类型实例。在这种情况下,它选择以 Stream 作为参数的第一个方法,但是您可以更改此谓词 method.GetParameters()。Any(par => par.ParameterType == typeof(Stream))到您想要的任何地方

Try to use next code snippet to see if it meets your need. It creates a close typed instance of method public static void Serialize<T>(Stream destination, T instance). In this case it select the first method with Stream as parameter, but you can change this predicate method.GetParameters().Any(par => par.ParameterType == typeof(Stream)) to whatever you want

public static object DeserializeReflection(Stream stream, object instance)
{
   return typeof(Serializer)
        .GetMethods()
        .First(method => method.Name == "Serialize" && method.GetParameters().Any(par => par.ParameterType == typeof(Stream)))
        .MakeGenericMethod(instance.GetType())
        .Invoke(null, new object[] { stream, instance });
}

这篇关于使用System.Type调用泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆