Skip to content

Commit 2ca115f

Browse files
author
Ron Petrusha
authored
Getting generic types with reflection (dotnet#10595)
* Getting generic types with reflection * Update docs/framework/reflection-and-codedom/specifying-fully-qualified-type-names.md Co-Authored-By: rpetrusha <ronpet@microsoft.com>
1 parent 5ed443d commit 2ca115f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

docs/framework/reflection-and-codedom/specifying-fully-qualified-type-names.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Specifying Fully Qualified Type Names"
3-
ms.date: "03/14/2018"
3+
ms.date: "02/21/2019"
44
helpviewer_keywords:
55
- "names [.NET Framework], fully qualified type names"
66
- "reflection, fully qualified type names"
@@ -17,10 +17,10 @@ ms.assetid: d90b1e39-9115-4f2a-81c0-05e7e74e5580
1717
author: "rpetrusha"
1818
ms.author: "ronpet"
1919
---
20-
# Specifying Fully Qualified Type Names
20+
# Specifying fully qualified type names
2121
You must specify type names to have valid input to various reflection operations. A fully qualified type name consists of an assembly name specification, a namespace specification, and a type name. Type name specifications are used by methods such as <xref:System.Type.GetType%2A?displayProperty=nameWithType>, <xref:System.Reflection.Module.GetType%2A?displayProperty=nameWithType>, <xref:System.Reflection.Emit.ModuleBuilder.GetType%2A?displayProperty=nameWithType>, and <xref:System.Reflection.Assembly.GetType%2A?displayProperty=nameWithType>.
2222

23-
## Grammar for Type Names
23+
## Grammar for type names
2424
The grammar defines the syntax of formal languages. The following table lists lexical rules that describe how to recognize a valid input. Terminals (those elements that are not further reducible) are shown in all uppercase letters. Nonterminals (those elements that are further reducible) are shown in mixed-case or singly quoted strings, but the single quote (') is not a part of the syntax itself. The pipe character (&#124;) denotes rules that have subrules.
2525

2626
```antlr
@@ -35,10 +35,13 @@ ReferenceTypeSpec
3535
3636
SimpleTypeSpec
3737
: PointerTypeSpec
38-
| ArrayTypeSpec
38+
| GenericTypeSpec
3939
| TypeName
4040
;
4141
42+
GenericTypeSpec
43+
: SimpleTypeSpec ` NUMBER
44+
4245
PointerTypeSpec
4346
: SimpleTypeSpec '*'
4447
;
@@ -101,7 +104,7 @@ AssemblyProperty
101104
;
102105
```
103106

104-
## Specifying Special Characters
107+
## Specifying special characters
105108
In a type name, IDENTIFIER is any valid name determined by the rules of a language.
106109

107110
Use the backslash (\\) as an escape character to separate the following tokens when used as part of IDENTIFIER.
@@ -125,7 +128,7 @@ AssemblyProperty
125128

126129
If the namespace were `Ozzy.Out+Back`, then the plus sign must be preceded by a backslash. Otherwise, the parser would interpret it as a nesting separator. Reflection emits this string as `Ozzy.Out\+Back.Kangaroo+Wallaby,MyAssembly`.
127130

128-
## Specifying Assembly Names
131+
## Specifying assembly names
129132
The minimum information required in an assembly name specification is the textual name (IDENTIFIER) of the assembly. You can follow the IDENTIFIER by a comma-separated list of property/value pairs as described in the following table. IDENTIFIER naming should follow the rules for file naming. The IDENTIFIER is case-insensitive.
130133

131134
|Property name|Description|Allowable values|
@@ -171,22 +174,24 @@ com.microsoft.crypto, Culture="", PublicKeyToken=a5d015c7d5a0b012
171174
com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012,
172175
Version=1.0.0.0
173176
```
174-
175-
## Specifying Pointers
177+
## Specifying generic types
178+
179+
SimpleTypeSpec\`NUMBER represents an open generic type with from 1 to *n* generic type parameters. For example, to get reference to the open generic type List\<T> or the closed generic type List\<String>, use ``Type.GetType("System.Collections.Generic.List`1")`` To get a reference to the generic type Dictionary\<TKey,TValue>, use ``Type.GetType("System.Collections.Generic.Dictionary`2")``.
180+
181+
## Specifying pointers
176182
SimpleTypeSpec* represents an unmanaged pointer. For example, to get a pointer to type MyType, use `Type.GetType("MyType*")`. To get a pointer to a pointer to type MyType, use `Type.GetType("MyType**")`.
177183

178-
## Specifying References
184+
## Specifying references
179185
SimpleTypeSpec & represents a managed pointer or reference. For example, to get a reference to type MyType, use `Type.GetType("MyType &")`. Note that unlike pointers, references are limited to one level.
180186

181-
## Specifying Arrays
187+
## Specifying arrays
182188
In the BNF Grammar, ReflectionEmitDimension only applies to incomplete type definitions retrieved using <xref:System.Reflection.Emit.ModuleBuilder.GetType%2A?displayProperty=nameWithType>. Incomplete type definitions are <xref:System.Reflection.Emit.TypeBuilder> objects constructed using <xref:System.Reflection.Emit?displayProperty=nameWithType> but on which <xref:System.Reflection.Emit.TypeBuilder.CreateType%2A?displayProperty=nameWithType> has not been called. ReflectionDimension can be used to retrieve any type definition that has been completed, that is, a type that has been loaded.
183189

184190
Arrays are accessed in reflection by specifying the rank of the array:
185191

186192
- `Type.GetType("MyArray[]")` gets a single-dimension array with 0 lower bound.
187193

188194
- `Type.GetType("MyArray[*]")` gets a single-dimension array with unknown lower bound.
189-
190195
- `Type.GetType("MyArray[][]")` gets a two-dimensional array's array.
191196

192197
- `Type.GetType("MyArray[*,*]")` and `Type.GetType("MyArray[,]")` gets a rectangular two-dimensional array with unknown lower bounds.

0 commit comments

Comments
 (0)