How do I extend a class with c# extension methods?

后端 未结 9 1283
情书的邮戳
情书的邮戳 2020-12-04 10:48

Can extension methods be applied to the class?

For example, extend DateTime to include a Tomorrow() method that could be invoked like:

DateTime.Tomor         


        
相关标签:
9条回答
  • 2020-12-04 11:03

    Unfortunately, you can't do that. I believe it would be useful, though. It is more natural to type:

    DateTime.Tomorrow
    

    than:

    DateTimeUtil.Tomorrow
    

    With a Util class, you have to check for the existence of a static method in two different classes, instead of one.

    0 讨论(0)
  • 2020-12-04 11:05

    You cannot add methods to an existing type unless the existing type is marked as partial, you can only add methods that appear to be a member of the existing type through extension methods. Since this is the case you cannot add static methods to the type itself since extension methods use instances of that type.

    There is nothing stopping you from creating your own static helper method like this:

    static class DateTimeHelper
    {
        public static DateTime Tomorrow
        {
            get { return DateTime.Now.AddDays(1); }
        }
    }
    

    Which you would use like this:

    DateTime tomorrow = DateTimeHelper.Tomorrow;
    
    0 讨论(0)
  • 2020-12-04 11:14

    I would do the same as Kumu

    namespace ExtensionMethods
    {
        public static class MyExtensionMethods
        {
            public static DateTime Tomorrow(this DateTime date)
            {
               return date.AddDays(1);
            }    
        }
    }
    

    but call it like this new DateTime().Tomorrow();

    Think it makes more seens than DateTime.Now.Tomorrow();

    0 讨论(0)
  • 2020-12-04 11:15

    I was looking for something similar - a list of constraints on classes that provide Extension Methods. Seems tough to find a concise list so here goes:

    1. You can't have any private or protected anything - fields, methods, etc.

    2. It must be a static class, as in public static class....

    3. Only methods can be in the class, and they must all be public static.

    4. You can't have conventional static methods - ones that don't include a this argument aren't allowed.

    5. All methods must begin:

      public static ReturnType MethodName(this ClassName _this, ...)

    So the first argument is always the this reference.

    There is an implicit problem this creates - if you add methods that require a lock of any sort, you can't really provide it at the class level. Typically you'd provide a private instance-level lock, but it's not possible to add any private fields, leaving you with some very awkward options, like providing it as a public static on some outside class, etc. Gets dicey. Signs the C# language had kind of a bad turn in the design for these.

    The workaround is to use your Extension Method class as just a Facade to a regular class, and all the static methods in your Extension class just call the real class, probably using a Singleton.

    0 讨论(0)
  • 2020-12-04 11:17

    The closest I can get to the answer is by adding an extension method into a System.Type object. Not pretty, but still interesting.

    public static class Foo
    {
        public static void Bar()
        {
            var now = DateTime.Now;
            var tomorrow = typeof(DateTime).Tomorrow();
        }
    
        public static DateTime Tomorrow(this System.Type type)
        {
            if (type == typeof(DateTime)) {
                return DateTime.Now.AddDays(1);
            } else {
                throw new InvalidOperationException();
            }
        }
    }
    

    Otherwise, IMO Andrew and ShuggyCoUk has a better implementation.

    0 讨论(0)
  • 2020-12-04 11:17

    We have improved our answer with detail explanation.Now it's more easy to understand about extension method

    Extension method: It is a mechanism through which we can extend the behavior of existing class without using the sub classing or modifying or recompiling the original class or struct.

    We can extend our custom classes ,.net framework classes etc.

    Extension method is actually a special kind of static method that is defined in the static class.

    As DateTime class is already taken above and hence we have not taken this class for the explanation.

    Below is the example

    //This is a existing Calculator class which have only one method(Add)

    public class Calculator 
    {
        public double Add(double num1, double num2)
        {
            return num1 + num2;
        }
    
    }
    
    // Below is the extension class which have one extension method.  
    public static class Extension
    {
        // It is extension method and it's first parameter is a calculator class.It's behavior is going to extend. 
        public static double Division(this Calculator cal, double num1,double num2){
           return num1 / num2;
        }   
    }
    
    // We have tested the extension method below.        
    class Program
    {
        static void Main(string[] args)
        {
            Calculator cal = new Calculator();
            double add=cal.Add(10, 10);
            // It is a extension method in Calculator class.
            double add=cal.Division(100, 10)
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题