In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of attributes and features in C#, which is very detailed and has a certain reference value. Friends who are interested must read it!
The first thing to say is that some people who are new to C # often confuse Property with Attribute. In fact, these are two different things. An attribute is a data field encapsulated in a class in object-oriented thinking, in the form of:
The fields that appear in the HumanBase class are called Property, but what about the C # Attribute?
To put it simply, we added a [Serializable] to the last line of the HumanBase class declaration, which is Attribute, which means that HumanBase can be serialized, which is important for network transmission, but you don't have to worry about how to understand it, which is what we will discuss next.
The C # feature can be applied to various types and members. In the previous example, when a property is applied to a class, it can be called a "class attribute". Similarly, if it is added to a method declaration, it is called a method attribute. No matter where they are used, no matter what the difference between them, the main purpose of the feature is self-description. And because the features can be customized by yourself, not limited to the ready-made ones provided by .NET, it brings considerable flexibility and convenience to the development of C# programs.
Let's borrow examples from life to introduce the feature mechanism of C#.
Suppose that one day you go to take a plane, you must go to the airport check-in office in advance to change your boarding pass. A boarding pass is a piece of paper that says which flight, where to fly, as well as your name, seat number and so on. In fact, this is the characteristic. It doesn't require you to physically include these attributes (humans didn't have airplanes when they appeared), just like the HumanBase class above doesn't have attributes like IsSerializable, and features just need to be added when the class or method needs them, just like you don't always fly in the sky.
When we want to know if HumanBase is serializable, we can do this by:
Getting your boarding pass means you can board the plane and take off legally. But at this time, you don't know where your plane is parked, don't worry, the ground crew will drive you there, but how does he know which flight you're on? Obviously through your boarding pass. Therefore, the feature * is characterized by self-description.
Since it plays the role of description, the purpose is to limit. Just like the ground crew won't just drag you to a plane and throw it on it, because the description on the label is limited, defining the destination, passengers and flights, and any error is considered abnormal. If the previous HumanBase does not add the Serializable feature, it cannot be transmitted on the network.
Let's introduce the characteristics of the method in the sequence, first adding a Run method to HumanProperty:
As long as you are able-bodied and healthy, you can run, so running is a prerequisite, at least with sound limbs and good health. Thus it can be seen that the disabled and the elderly will have problems if they run. Suppose that the object of a HumanBase represents an octogenarian, and it would be honorable to let him be Liu Xiang's sparring partner. How to avoid this situation? we can add a piece of logic code to the Run method to determine the Age size first. If it is less than 2 or greater than 60, an exception is thrown directly, but between 2 and 60 years old, we also have to use Switch to determine whether the speed parameters are appropriate by age, so the logic is quite bloated. In short, how do you use features to indicate that a method cannot be used? OK, here we go:
The above gives an overview of the use and function of features, and then we will show you how to improve the flexibility of the program through custom features. If the feature mechanism can only use the features provided by .NET, it's not very enjoyable.
First of all, properties are also classes. Unlike other classes, properties must be inherited from the System.Attribute class, otherwise how does the compiler know who is the feature and who is the normal class. When the compiler detects that a class is a property, it recognizes the information in it and stores it in the metadata, that's all, the compiler doesn't care what the feature says, and the feature doesn't have any effect on the compiler. just as airlines don't care where each box is going, only the owner and porter of the box care about these details. Assuming that we are now airline managers and need to design the boarding pass mentioned above, then it is very simple, let's take a look at the main information:
We simply list these attributes as the information on the airline boarding pass, using the same as the previous one, and just post it on HumanBase, indicating that the person is qualified to board the plane. To mention briefly here, you may have noticed that Attribute has been omitted when using BoardingCheckAttribute, don't worry, this is the right thing to do, because the compiler will add and look for this property class by default. Oh, wait a minute, it suddenly occurred to me which plane he should board. Obviously, in this demand, our characteristics have not played a due role, we still have to do some work, otherwise passengers will be very confused in the face of a blank ticket.
Therefore, we must add a constructor to this C# feature, because it not only indicates the eligibility to board the plane, but also contains some necessary information:
OK, our passengers can get an official boarding pass to board the plane, have a good flight!
The above is all the content of the article "sample Analysis of attributes and Features in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.