代码之家  ›  专栏  ›  技术社区  ›  Kuker

你能自动传递“this”作为参数吗?

  •  0
  • Kuker  · 技术社区  · 7 年前

    有自动通过的选项吗 this 作为函数的参数?例如,您可以使用 [CallerMemberName] 自动通过 string .

    void PassMeThis([AutomaticThisParam] object source);
    

    使用方法:

    public class SomeClass{    
        void SomeMethod(){
            PassMeThis(); // instead of PassMeThis(this)
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   Willie    7 年前

    1. 创建静态类 Public static class MethodExtensions

    2. 像这样添加一个静态方法 public static PassMeThis(this SomeClass model) { // }

    3. 像这样调用方法(参考您的代码示例): this.PassMeThis()

    而且是自动填充的。 我希望这就是你想要的

        2
  •  1
  •   Marcell Toth    7 年前

    PassMeThis 同班同学 (或祖先),在你的例子中,这是没有意义的,这在那里也是可用的。

    如果是的话 ,有 自动获取调用对象引用的方法: IS there any way to get a reference to the calling object in c#?