How to call an extension method without using

后端 未结 7 1185
旧时难觅i
旧时难觅i 2021-01-03 22:37
using System;

class Runner
{
    static void Main()
    {
        A a = new A();
        // how to say a.PrintStuff() without a \'using\'
        Console.Read();
           


        
7条回答
  •  灰色年华
    2021-01-03 22:44

    It is possible to directly call your extension like so since it is simply a static method, passing the instance it will act on as the first this parameter:

    A a = new A();
    ExtensionMethod.AExtensions.PrintStuff(a);
    

    This might be confusing to other developers who happen across this code if you followed this pattern for more commonly used extension methods. It would also make chaining extension calls such as LINQ appear more like a functional language because you would be nesting each call instead of chaining them.

提交回复
热议问题