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

What are the advantages and limitations of the .NET 3.5 extension method

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

Share

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

What are the advantages and limitations of the .NET 3.5 extension method? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

.net 3.5 extension method: new language features

Originally, when I heard about Partial Class, I thought I could add methods to existing classes, but the requirement that Partial Class can only be in the same Assembly makes it impossible to achieve this. .net 3.5 adds many language features, and the extension method is one of them. With extension methods, we can add methods to existing classes.

Public static class ExtendTest {public static Print (this object o) {Console.WriteLine (o);}}

You can then use this method for any object:

String s = "hello"; s.Print (); "X" .print (); MyClass x = new MyClass (); x.Print ()

Creation of .NET 3.5 extension method

Basically, there are only two things to remember about the creation of an extension method: it must be a static method, and the this keyword must be added with * * parameters.

I looked at it with Reflector, which is basically consistent with my guess, but its implementation is actually using custom properties, so the extension method can basically be seen as a compiler feature:

Public static class ExtendTest {[Extend] public static Print (object o) {Console.WriteLine (o);}}

The extension method is easy to use and can indeed be compared with dynamic languages such as Python and Ruby. However, after all, C # is a static language, and there are some limitations to the extension method.

Limitations of the .NET 3.5 extension method

One limitation is that private or protected members of the original class cannot be used. This problem, how to put it, is fine in most cases, but it will be a headache if you really want to use private ownership or protect members. If you want to add methods to classes in .net framework, this problem may not be too big, after all, we generally only know their public members. If you want to add a new DLL to your DLL, and make sure that the original DLL is used for .net 2.0 and the new DLL extends the original DLL, you will encounter it. My solution to this problem in DbEntry.Net is to use friend assemblies. This method still needs to modify the original DLL, add the new DLL as the friend assembly, and mark the members that need to be accessed as internal.

Another limitation: you cannot add static methods to a class. I don't know if I didn't find it. Anyway, it feels like it's already a static method. How can I label it as a static method? Do you use static static? Of course, there should not be many scenarios used.

One more restriction: you cannot add an interface to a class. This is not the design goal of the extension method in the first place, and it seems to be a lot of trouble to implement, so you shouldn't complain, but if it can be achieved, there will be a lot of convenience. For example, if you can add an IQueryable interface to DbObjectModel, you can achieve all the Linq functions by adding only one using, and when you have this limitation, you can only implement one more LinqDbObjectModel and so on.

The answers to the questions about the advantages and limitations of the .NET 3.5 extension method are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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