How to call an extension method without using

后端 未结 7 1127
旧时难觅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:50

    This makes me feel dirty, but you can put your extension methods in the System namespace. This namespace is included by default in your question

    using System;
    
    class Runner
    {
        static void Main()
        {
            A a = new A();
            // how to say a.PrintStuff() without a 'using'
            Console.Read();
        }
    }
    
    class A { }
    
    namespace System
    {
        static class AExtensions
        {
            public static void PrintStuff(this A a)
            {
                Console.WriteLine("text");
            }
        }
    }
    

提交回复
热议问题