Extension Method for Extending a Static Class

I am wondering if I can use the extension method or other methods to extend the static class, for example System.Net.Mime.MediaTypeNames.Image, it has fewer types than I need.

+5
source share
2 answers

No, extension methods can only be used to add instance methods, not static methods (or even properties). Extension methods are just syntactic sugar around static methods. For example, when you use an extension method, for example Count ():

var list = GetList();
var size = list.Count();

It is actually compiled for:

var list = GetList();
var size = Enumerable.Count(list);

You cannot add additional static methods to an existing class using extension methods.

+6

, #, , , - . , object. , , , , , . :

public static class MessageBox2
{
    public static DialogResult ShowError(string Caption, string Message, params object[] OptionalFormatArgs)
    {
        return MessageBox.Show(string.Format(Message, OptionalFormatArgs), Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

, "" this .

0

All Articles