<bdo id='QJlZ0'></bdo><ul id='QJlZ0'></ul>
    1. <legend id='QJlZ0'><style id='QJlZ0'><dir id='QJlZ0'><q id='QJlZ0'></q></dir></style></legend>

    2. <small id='QJlZ0'></small><noframes id='QJlZ0'>

      <i id='QJlZ0'><tr id='QJlZ0'><dt id='QJlZ0'><q id='QJlZ0'><span id='QJlZ0'><b id='QJlZ0'><form id='QJlZ0'><ins id='QJlZ0'></ins><ul id='QJlZ0'></ul><sub id='QJlZ0'></sub></form><legend id='QJlZ0'></legend><bdo id='QJlZ0'><pre id='QJlZ0'><center id='QJlZ0'></center></pre></bdo></b><th id='QJlZ0'></th></span></q></dt></tr></i><div id='QJlZ0'><tfoot id='QJlZ0'></tfoot><dl id='QJlZ0'><fieldset id='QJlZ0'></fieldset></dl></div>
      <tfoot id='QJlZ0'></tfoot>

        具有未知类型的 CreateDelegate

        CreateDelegate with unknown types(具有未知类型的 CreateDelegate)

          <small id='6cBLL'></small><noframes id='6cBLL'>

          <i id='6cBLL'><tr id='6cBLL'><dt id='6cBLL'><q id='6cBLL'><span id='6cBLL'><b id='6cBLL'><form id='6cBLL'><ins id='6cBLL'></ins><ul id='6cBLL'></ul><sub id='6cBLL'></sub></form><legend id='6cBLL'></legend><bdo id='6cBLL'><pre id='6cBLL'><center id='6cBLL'></center></pre></bdo></b><th id='6cBLL'></th></span></q></dt></tr></i><div id='6cBLL'><tfoot id='6cBLL'></tfoot><dl id='6cBLL'><fieldset id='6cBLL'></fieldset></dl></div>
        1. <tfoot id='6cBLL'></tfoot>

            • <bdo id='6cBLL'></bdo><ul id='6cBLL'></ul>
                    <tbody id='6cBLL'></tbody>
                  <legend id='6cBLL'><style id='6cBLL'><dir id='6cBLL'><q id='6cBLL'></q></dir></style></legend>

                  本文介绍了具有未知类型的 CreateDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建委托以在运行时读取/写入未知类型类的属性.

                  I am trying to create Delegate for reading/writing properties of unknown type of class at runtime.

                  我有一个通用类 Main<T> 和一个看起来像这样的方法:

                  I have a generic class Main<T> and a method which looks like this:

                  Delegate.CreateDelegate(typeof(Func<T, object>), get)
                  

                  其中 get 是应读取的属性的 MethodInfo.问题是当属性返回 int 时(我猜这发生在值类型上)上面的代码抛出 ArgumentException 因为方法不能被绑定.如果是字符串,它工作得很好.

                  where get is a MethodInfo of the property that should be read. The problem is that when the property returns int (I guess this happens for value types) the above code throws ArgumentException because the method cannot be bound. In case of string it works well.

                  为了解决这个问题,我更改了代码,以便使用 MakeGenericType 生成相应的委托类型.所以现在的代码是:

                  To solve the problem I changed the code so that corresponding Delegate type is generated by using MakeGenericType. So now the code is:

                  Type func = typeof(Func<,>);
                  Type generic = func.MakeGenericType(typeof(T), get.ReturnType);
                  var result = Delegate.CreateDelegate(generic, get)
                  

                  现在的问题是 generic 的创建委托实例,所以我必须使用 DynamicInvoke 这与使用纯反射读取字段一样慢.

                  The problem now is that the created delegate instance of generic so I have to use DynamicInvoke which would be as slow as using pure reflection to read the field.

                  所以我的问题是为什么第一段代码会因值类型而失败.根据 MSDN 它应该像它所说的那样工作

                  So my question is why is that the first snippet of code fails with value types. According to MSDN it should work as it says that

                  如果方法的返回类型比委托的返回类型更严格,则委托的返回类型与方法的返回类型兼容

                  The return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate

                  以及如何在第二个片段中执行委托,使其比反射更快.

                  and how to execute the delegate in the second snippet so that it is faster than reflection.

                  谢谢.

                  推荐答案

                  这是解决问题的一种方法.创建一个泛型方法:

                  Here's one way to solve your problem. Create a generic method:

                  public static Func<T, object> MakeDelegate<U>(MethodInfo @get)
                  {
                      var f = (Func<T, U>)Delegate.CreateDelegate(typeof(Func<T, U>), @get);
                      return t => f(t);
                  }
                  

                  这样,C# 的编译器负责插入必要的装箱(如果有)以将 f(t)(类型为 U)转换为 object.现在你可以使用反射来调用这个 MakeDelegate 方法,并将 U 设置为 @get.ReturnType,你得到的将是一个 >Func 无需使用 DynamicInvoke 即可调用.

                  This way, C#'s compiler takes care of inserting the necessary boxing (if any) to convert f(t) (of type U) to object. Now you can use reflection to call this MakeDelegate method with U set to @get.ReturnType, and what you get back will be a Func<T, object> which can be called without needing to resort to using DynamicInvoke.

                  这篇关于具有未知类型的 CreateDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <tbody id='2XOzs'></tbody>
                  <legend id='2XOzs'><style id='2XOzs'><dir id='2XOzs'><q id='2XOzs'></q></dir></style></legend>
                  <i id='2XOzs'><tr id='2XOzs'><dt id='2XOzs'><q id='2XOzs'><span id='2XOzs'><b id='2XOzs'><form id='2XOzs'><ins id='2XOzs'></ins><ul id='2XOzs'></ul><sub id='2XOzs'></sub></form><legend id='2XOzs'></legend><bdo id='2XOzs'><pre id='2XOzs'><center id='2XOzs'></center></pre></bdo></b><th id='2XOzs'></th></span></q></dt></tr></i><div id='2XOzs'><tfoot id='2XOzs'></tfoot><dl id='2XOzs'><fieldset id='2XOzs'></fieldset></dl></div>

                        <bdo id='2XOzs'></bdo><ul id='2XOzs'></ul>
                          <tfoot id='2XOzs'></tfoot>

                          <small id='2XOzs'></small><noframes id='2XOzs'>