Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the generic Attribute of Category 8

Shulou Source: shulou.com Published: 2022-06-01 18:35:41 10月07日 Update

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!

Tags: Run function compile theory limit content parameter actual tool proposal type compiler error problem support function local property is in the state Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Redmi vpn Huawei