Some fragmentation of generic reflection skills

xiaoxiao2021-04-10  531

This article is a simple column of some generic reflection techniques, preparing for future articles.

1. How to get a TYPE object of a closed constructed type?

It is assumed that the following types:

Class

TestType

<

T

>

Class

TestType

<

T, U

>

If you want to get a TYPE object that is closed, you only need to use the C # TypeOf operator, or the VB's GetType operator can act on the specific type:

//

C #

Type T1

=

Typeof

(TestType

<

int

>

);

'

VB

DIM

T2

AS

Type

=

Gettype

(TestType

String

))

2. How do I get a generic type? TYPE object?

The so-called generic type is a type of parameters, but the type parameters have not yet specified original definitions. We cannot use TestType such syntax because t does not exist in our context. At this time, it can be obtained with empty angle brackets (C #) or empty OF statements (VB).

Type T1

=

Typeof

(TestType

<>

); TYPE T2

=

Typeof

(TestType

<

,

>

);

DIM

T1, T2

AS

Typet1

=

Gettype

(TESTTYPE (OF)) T2

=

Gettype

(TESTTTYPE (OF,))

Note that we can use a comma to distinguish the number of type parameters. This shows that the generic type can only be overloaded by the type parameters, regardless of what constraints. The TYPE obtained here is the generic type of type parameters.

3. How to generate a generic type TYPE object from the TYPE object of the constructor?

The new method of the Type class can do it.

//

C #

Type CT

=

Typeof

(List)

<

int

>

);

//

Get Generic Type Definition

Type gt

=

Ct.getGenericTyPedefinition ();

4. How do I get the TYPE object for type parameters?

The generic type T, U and other types of parameters, and the actual values ​​in the run are available from the TYPE object.

'

VB

DIM

t

AS

Type

=

Gettype

(List

Integer

))

'

Get the generic arguments, an array

DIM

Typeargs

AS

TYPE ()

=

T.GetGenericarguments ()

'

Get the first argument: Integer in this case

DIM

Targ0

AS

Type

=

Typeargs

0

)

5, generate Type objects of the constructed type from the generic type Type object.

It is usually used to generate another constructical type from a construction type.

//

C #

Type CT

=

Typeof

(List)

<

int

>

Type GT

=

Ct.getGenericTyPedefinition ();

//

Make Another Constructed Type

//

The List in this case

Type CT2

=

gt.makegenerictype (Typeof

(

String

));

6, how to take an open constructive type (Open constructed type) TYPE object?

Open consigne is one of the most difficult processes because their type parameters have been specified, but not specified as specific types, but specified as type parameters for other generic types. This type is especially important when performing reflection overload selection and reflection emit. Our method is to get the type of type parameters from the definition of the host generic type, and then build an open consignment type. Here, we get the parameters of the constructor of the list , the type of IEnumerable , note that the T here is defined by List , not the generic IEnumerable own type parameters

'

The Generic Type of List (of t)

DIM

TLIST

AS

Type

=

Gettype

(List (of))

'

Get the "t" of list (of t)

DIM

TypeParam

AS

Type

=

TList.GetGenericarguments () ()

0

)

'

The Generic Type of Ienumerable (of t)

DIM

Tienum

AS

Type

=

Gettype

(IEnumerable (OF))

'

Make the Open Constructed Type

DIM

Tienumopen

AS

Type

=

Tienum.makegenericType (TypeParam)

'

Only use this method to achieve open constructive type

'

You can use this syntax to get the truly desired constructor definition

'

IENUMERABLE (OF T) is an open consignment type in the definition of constructor

DIM

c

AS

Constructorinfo

=

_ TList.getConstructor (

New

Type () {tienumopen})

转载请注明原文地址:https://www.9cbs.com/read-133362.html

New Post(0)