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

        <small id='zlQ19'></small><noframes id='zlQ19'>

        <tfoot id='zlQ19'></tfoot>
          <bdo id='zlQ19'></bdo><ul id='zlQ19'></ul>

      1. 从属性 getter 或 setter 方法创建委托

        时间:2023-11-11

        <legend id='hDqHs'><style id='hDqHs'><dir id='hDqHs'><q id='hDqHs'></q></dir></style></legend>
        <tfoot id='hDqHs'></tfoot>

            <bdo id='hDqHs'></bdo><ul id='hDqHs'></ul>

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

                    <tbody id='hDqHs'></tbody>
                1. <small id='hDqHs'></small><noframes id='hDqHs'>

                  本文介绍了从属性 getter 或 setter 方法创建委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  要从方法创建委托,您可以使用编译类型安全语法:

                  To create a delegate from a method you can use the compile type-safe syntax:

                  private int Method() { ... }
                  
                  // and create the delegate to Method...
                  Func<int> d = Method;
                  

                  属性是 getter 和 setter 方法的包装,我想为属性 getter 方法创建一个委托.类似的东西

                  A property is a wrapper around a getter and setter method, and I want to create a delegate to a property getter method. Something like

                  public int Prop { get; set; }
                  
                  Func<int> d = Prop;
                  // or...
                  Func<int> d = Prop_get;
                  

                  不幸的是,这不起作用.我必须创建一个单独的 lambda 方法,当 getter 方法与委托签名匹配时,这似乎是不必要的:

                  Which doesn't work, unfortunately. I have to create a separate lambda method, which seems unnecessary when the getter method matches the delegate signature anyway:

                  Func<int> d = () => Prop;
                  

                  为了直接使用委托方法,我必须使用讨厌的反射,这不是编译类型安全的:

                  In order to use the delegate method directly, I have to use nasty reflection, which isn't compile type-safe:

                  // something like this, not tested...
                  MethodInfo m = GetType().GetProperty("Prop").GetGetMethod();
                  Func<int> d = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), m);
                  

                  有没有什么方法可以直接以编译安全的方式在属性获取方法上创建委托,类似于在顶部的普通方法上创建委托,而无需使用中间 lambda 方法?

                  Is there any way of creating a delegate on a property getting method directly in a compile-safe way, similar to creating a delegate on a normal method at the top, without needing to use an intermediate lambda method?

                  推荐答案

                  据我所知,您已经写下了所有有效"的变体.由于无法在普通代码中显式处理 getter 或 setter(即没有反射),我认为没有办法做你想做的事.

                  As far as I can tell, you have already written down all "valid" variants. Since it isn't possible to explicitly address a getter or setter in normal code (without reflection, that is), I don't think that there is a way to do what you want.

                  这篇关于从属性 getter 或 setter 方法创建委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为事件定义一个空的委托主体是一个好习惯吗? 下一篇:表达式树和调用委托

                  相关文章

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

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

                    <tfoot id='yOUos'></tfoot>
                      • <bdo id='yOUos'></bdo><ul id='yOUos'></ul>