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 understand the generic Attribute of Category 8

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

Share

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

The main content of this article is to explain "how to understand the Category 8 generic Attribute". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand the Categ8 generic Attribute".

From the beginning, Attribute has been part of .NET. Therefore, they are created before generics are introduced, which means that if you want to reference a type in a property, you must expose a Type parameter or property. For example:

[TypeConverter (typeof (X))]

There are some limitations to this model. In the above example, X must have a constructor without arguments and inherit TypeConverter. The compiler does not enforce these, so developers can only find errors at run time.

On the contrary, if we use generic type parameters, these rules can be enforced in the compiler and the grammar will be slightly clearer.

[TypeConverter]

Theoretically, the "Allow Generic Attributes" proposal is a very simple change. CLR already supports this concept, and the question is how to use it in the. NET language But sometimes theory and reality are two parallel lines, and even if CLR can support it, it does not necessarily mean it will support it. Jan Kotas explained:

I think the nature of this problem is similar to local references and return. Local references and return are not subject to any API restrictions, and in theory they work fine on existing runtimes, tools, and libraries. But in practice, they may not work properly because they expose pre-existing errors and limitations, perhaps because their processing paths have never been executed, or because they are clearly not as expected. Errors and problems related to referencing return are just beginning to emerge because people start to use this feature and find something that doesn't work. I think this is within expectation. We should acknowledge these facts and not take the same action as we did before to withdraw the default type constructor function.

This function is very similar. ECMA does not target content that forbids the use of generic Attribute, which should work in theory, but in practice it does not. If you want to function properly, you must update many parts of the entire system:

There are multiple runtimes (Mono, CoreRT, entire framework)

Some IL tools may also be affected (compiler, Cecil-based, or CCI2-based tools)

You may need an API to access the generic Attribute (for example, the existing reflection API does not allow you to access the generic Attribute-- you have to enumerate).

One limitation of the proposal is that the Attribute feature must be "turned off" at compile time. Here is an example of enabling generic Attribute provided in the proposal.

[SomeAttribute] public void DoSomething (T input) {}

Because T is not defined at compile time, the reflection mechanism does not know what this expression will return.

Typeof (GenericClass) .GetMethod ("DoSomething") .GetCustomAttributes ()

Status

Currently, generic Attribute features are listed in the C # 8 roadmap with a status of "in Progress".

At this point, I believe you have a deeper understanding of "how to understand the Category 8 generic Attribute". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report