Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the extension method of C#

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve the expansion of C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In our programming career, we have to use a lot of class libraries, some of which are developed by ourselves, we have her code, and some are released by third parties, and we not only don't have their code, we don't even have a chance to see it.

As. Net programmers, we deal with BCL (Base Class Linbrary) every day. There is no doubt that BCL is successful as a young framework class library, but sometimes we still have to write some "Helper" methods to extend the class library, because we can't modify the source code of the class library, we have to write static classes one by one. Although it is convenient to use, it is always a bit indecent for programmers who pursue it. Now this is what happened to me. Two days ago, I was ordered to write a method of loading Chart diagrams from XML files, loading data from XML and binding to objects. This must be a chance for reflection to show its ability. I often need to write something based on the object property name to determine whether the object has this property or get the value of the property based on the property name. As usual, I quickly wrote a PropertyHelper with two static methods: HasProperty,GetValueByName.

PropertyHelper.HasProperty (point, "X"), such a call is OK, but Microsoft provides us with a C# extension method in C# 3.0. Now we can call point.HasProperty ("X") directly like this; see how I implement this C # extension method?

Public static class PropertyExtension {public static object GetValueByName (this object self, string propertyName) {if (self = = null) {return self;} Type t = self.GetType (); PropertyInfo p = t.GetProperty (propertyName); return p.GetValue (self, null);}}

I added a C # extension method to the object type. In. Net, all classes inherit from object, so all classes have this method by default.

Notice how it differs from a normal static method? The this keyword is added before the * parameters of this method.

C# extension method:

1 the class in which the method is located must be static

2 methods must also be static

3 the * parameters of the method must be the type you want to extend, for example, if you want to extend a method to int, then the * parameters must be int.

4 A this keyword is required before the * parameters.

Follow the above steps to write and you get a "C# extension method", which you can call as if it were a native method of this class:

String str = "abc"; object len = str.GetValueByName ("Length")

It is as if the string type now has a GetValueByName method, but in fact string does not have such a method. And why is that? It was our lovely compiler that tampered with it. To avoid the interference of the compiler, let's directly appreciate the MSIL code:

Lhas0008: ldstr "Length" Lath000d: call object TestLambda.PropertyExtension::GetValueByName (object, string)

Here are a few principles to pay attention to when writing a C# extension method (of course, different people have different opinions, which is also the opinion of one family):

The C # extension method has the principle of proximity, that is, if there are two identical C # extension methods in your program, one is in the same namespace as your class, and the other is in another namespace, this time will give priority to using the C# extension method in the same namespace, that is, the closer the consanguinity is, the more favored it is.

Many people see that there may be a golden light in the eyes of the C# extension method, and it can be expanded regardless of whether it is thirty-seven or twenty-one in the future. Some people will extend the class at will, replacing all the previous methods as "Helper" with the C# extension method, and note that the C# extension method is "polluting", so I think when expanding, I still think about whether it is worth expanding.

When extending, do not extend the higher-level classes. For example, when I face the extension of object, I think it is not desirable. Object is the base class of all classes. Once extended, all classes are "contaminated".

This is the end of this article on "how to implement the extension method of C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report