Why does a generic .NET type have one entry in TypeDef and another open type in TypeSpec?

Why does a generic .NET type have one entry in TypeDef and another open type in TypeSpec?

Was it just an extended TypeDef with a signature when generics were added to .NET?

+3
source share
1 answer

Let's look at these two different types:

public class Foo<TFoo> {}

public class Fighter {}

For these two definitions, and just like any other type, there will be an entry in the TypeDef table. In this case, there will also be an entry in the GenericParam table linking Foo to TFoo.

Now, most of the time when you use Foo, you will not use its definition directly, because it is not very interesting, you will want to use an instance of Foo. Therefore, if you write:

new Foo<string> ();

TypeSpec , , Foo : string. , :

public class Bar<TBar> {
    public Foo<TBar> Foo;
}

TypeSpec, , Foo : TBar. TypeSpec , . :

var type = typeof (Fighter[,]);

TypeSpec, Fighter. :

var type = typeof (Foo<>);

Foo, , : TypeSpec . , :

var type = typeof (Foo<string>);

TypeSpec, .

, , TypeDef, : (TypeSpec).

, , GenericParam, , TFoo, GenericParamConstraints, , MethodSpec, , TypeSpec TypeDef: . , .

+6

Source: https://habr.com/ru/post/1788917/


All Articles