How to call an extension method without using

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

    You can add extensions method without namespace. This will affect the whole systems which is not recommended.

    public static class StringExtensions
    {       
        public static void HelloWorld(this String s)
        {
            Console.Write("Hello World");
        }
    }
    
    
    
        string str = "s";
        str.HelloWorld();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • Ruminations on the creation of extension methods for a type ExtendableType:

    • Name the class ExtendableTypeExtensions
    • Declare the extension class partial so that clients can add extension methods following the same pattern; and
    • Put the extension methods in the same namespace as the base type

    unless you have a very good reason to follow a model like that of LINQ:

    • A substantial family of extension methods,
    • That all apply to multiple base classes.
    0 讨论(0)
  • 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");
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 22:59

    that is possible if Extension Method and class A in same namespace,

    If you have to use different namespaces then you have to use using, i don't think there is a way to do this without using. But you may reduce the number of using by putting all the extensions in one namespace like for Linq (System.Linq.Extensions)

    Note : You can remove the namespace for Extension methods, then it will make them globally available

    0 讨论(0)
  • 2021-01-03 23:03

    In our projects extensions are placed in the same namespace as class extension for. Your example:

    A.cs:
    using System;
    namespace ANamespace
    {
    
         class A { }
    }
    
    AExtensions.cs:
    namespace ANamespace
    {
        static class AExtensions
        {
            public static void PrintStuff(this A a)
            {
                Console.WriteLine("text");
            }
        }
    }
    

    Now when you add using for ANamespace for using the A class, all extensions for A class will be included too.

    0 讨论(0)
提交回复
热议问题