You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/framework/interop/marshalling-classes-structures-and-unions.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
title: "Marshalling Classes, Structures, and Unions"
3
3
description: Review how to marshal classes, structures, and unions. View samples of marshalling classes, structures with nested structures, arrays of structures, and unions.
4
4
ms.date: "03/30/2017"
5
-
dev_langs:
5
+
dev_langs:
6
6
- "csharp"
7
7
- "vb"
8
8
- "cpp"
9
-
helpviewer_keywords:
9
+
helpviewer_keywords:
10
10
- "data marshalling, classes"
11
11
- "marshaling, unions"
12
12
- "marshaling, structures"
@@ -42,8 +42,8 @@ The following table lists marshalling options for classes, structures, and union
42
42
43
43
## Structures sample
44
44
45
-
This sample demonstrates how to pass a structure that points to a second structure, pass a structure with an embedded structure, and pass a structure with an embedded array.
46
-
45
+
This sample demonstrates how to pass a structure that points to a second structure, pass a structure with an embedded structure, and pass a structure with an embedded array.
46
+
47
47
The Structs sample uses the following unmanaged functions, shown with their original function declaration:
48
48
49
49
-**TestStructInStruct** exported from PinvokeLib.dll.
@@ -222,7 +222,7 @@ The `NativeMethods` class contains the prototypes for the `TestUnion` and `TestU
222
222
223
223
In some scenarios, `struct` and `union` layouts can differ depending on the targeted platform. For example, consider the [`STRRET`](/windows/win32/api/shtypes/ns-shtypes-strret) type when defined in a COM scenario:
224
224
225
-
```c++
225
+
```cpp
226
226
#include<pshpack8.h>/* Defines the packing of the struct */
Copy file name to clipboardExpand all lines: docs/fundamentals/reflection/dynamically-loading-and-using-types.md
-4
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,12 @@ ms.date: "03/30/2017"
5
5
dev_langs:
6
6
- "csharp"
7
7
- "vb"
8
-
- "cpp"
9
8
helpviewer_keywords:
10
9
- "late binding, about late binding"
11
10
- "early binding"
12
11
- "dynamically loading and using types"
13
12
- "implicit late binding"
14
13
- "reflection, dynamically using types"
15
-
ms.assetid: db985bec-5942-40ec-b13a-771ae98623dc
16
14
---
17
15
# Dynamically load and use types
18
16
@@ -44,7 +42,6 @@ Using custom binding, you can load an assembly at run time, obtain information a
44
42
45
43
The following example demonstrates a simple custom binder that provides no argument type conversion. Code for `Simple_Type.dll` precedes the main example. Be sure to build `Simple_Type.dll` and then include a reference to it in the project at build time.
@@ -54,7 +51,6 @@ Use <xref:System.Type.InvokeMember%2A?displayProperty=nameWithType> to invoke a
54
51
55
52
The following example shows the three possible combinations of argument coercion (type conversion) and member selection. In Case 1, no argument coercion or member selection is needed. In Case 2, only member selection is needed. In Case 3, only argument coercion is needed.
Copy file name to clipboardExpand all lines: docs/fundamentals/reflection/how-to-define-a-generic-type-with-reflection-emit.md
-15
Original file line number
Diff line number
Diff line change
@@ -5,12 +5,10 @@ ms.date: 03/27/2024
5
5
dev_langs:
6
6
- "csharp"
7
7
- "vb"
8
-
- "cpp"
9
8
helpviewer_keywords:
10
9
- "generics [.NET], reflection emit"
11
10
- "generics [.NET], dynamic types"
12
11
- "reflection emit, generic types"
13
-
ms.assetid: 07d5f01a-7b5b-40ea-9b15-f21561098fe4
14
12
---
15
13
# How to: Define a generic type with reflection emit
16
14
@@ -23,51 +21,43 @@ This article shows how to create a simple generic type with two type parameters,
23
21
24
22
1. Define a dynamic assembly named `GenericEmitExample1`. In this example, the assembly is executed and saved to disk, so <xref:System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave?displayProperty=nameWithType> is specified.
1. Define a dynamic module. An assembly is made up of executable modules. For a single-module assembly, the module name is the same as the assembly name, and the file name is the module name plus an extension.
1. Define the generic type parameters of `Sample` by passing an array of strings containing the names of the parameters to the <xref:System.Reflection.Emit.TypeBuilder.DefineGenericParameters%2A?displayProperty=nameWithType> method. This makes the class a generic type. The return value is an array of <xref:System.Reflection.Emit.GenericTypeParameterBuilder> objects representing the type parameters, which can be used in your emitted code.
43
38
44
39
In the following code, `Sample` becomes a generic type with type parameters `TFirst` and `TSecond`. To make the code easier to read, each <xref:System.Reflection.Emit.GenericTypeParameterBuilder> is placed in a variable with the same name as the type parameter.
1. Add special constraints to the type parameters. In this example, type parameter `TFirst` is constrained to types that have parameterless constructors, and to reference types.
1. Optionally add class and interface constraints to the type parameters. In this example, type parameter `TFirst` is constrained to types that derive from the base class represented by the <xref:System.Type> object contained in the variable `baseType`, and that implement the interfaces whose types are contained in the variables `interfaceA` and `interfaceB`. See the code example for the declaration and assignment of these variables.
1. Define a field. In this example, the type of the field is specified by type parameter `TFirst`. <xref:System.Reflection.Emit.GenericTypeParameterBuilder> derives from <xref:System.Type>, so you can use generic type parameters anywhere a type can be used.
1. Define a method that uses the type parameters of the generic type. Note that such methods are not generic unless they have their own type parameter lists. The following code defines a `static` method (`Shared` in Visual Basic) that takes an array of `TFirst` and returns a `List<TFirst>` (`List(Of TFirst)` in Visual Basic) containing all the elements of the array. To define this method, it is necessary to create the type `List<TFirst>` by calling <xref:System.Type.MakeGenericType%2A> on the generic type definition, `List<T>`. (The `T` is omitted when you use the `typeof` operator (`GetType` in Visual Basic) to get the generic type definition.) The parameter type is created by using the <xref:System.Type.MakeArrayType%2A> method.
@@ -82,25 +72,21 @@ This article shows how to create a simple generic type with two type parameters,
82
72
83
73
Now it is possible to get the constructor of `List<T>` by calling <xref:System.Type.GetConstructor%2A> on the generic type definition. To convert this constructor to the corresponding constructor of `List<TFirst>`, pass `List<TFirst>` and the constructor from `List<T>` to the static <xref:System.Reflection.Emit.TypeBuilder.GetConstructor%28System.Type%2CSystem.Reflection.ConstructorInfo%29?displayProperty=nameWithType> method.
1. Invoke the method. `ExampleMethod` is not generic, but the type it belongs to is generic, so to get a <xref:System.Reflection.MethodInfo> that can be invoked, it's necessary to create a constructed type from the type definition for `Sample`. The constructed type uses the `Example` class, which satisfies the constraints on `TFirst` because it is a reference type and has a default parameterless constructor, and the `ExampleDerived` class which satisfies the constraints on `TSecond`. (The code for `ExampleDerived` can be found in the example code section.) These two types are passed to <xref:System.Type.MakeGenericType%2A> to create the constructed type. The <xref:System.Reflection.MethodInfo> is then obtained using the <xref:System.Type.GetMethod%2A> method.
1. The following code creates an array of `Example` objects, places that array in an array of type <xref:System.Object> representing the arguments of the method to be invoked, and passes them to the <xref:System.Reflection.MethodBase.Invoke%28System.Object%2CSystem.Object%5B%5D%29> method. The first argument of the <xref:System.Reflection.MethodBase.Invoke%2A> method is a null reference because the method is `static`.
@@ -118,7 +104,6 @@ The program includes a method that lists information about a generic type, and a
118
104
119
105
The program saves the finished module to disk as `GenericEmitExample1.dll`, so you can open it with the [Ildasm.exe (IL Disassembler)](../../framework/tools/ildasm-exe-il-disassembler.md) and examine the CIL for the `Sample` class.
0 commit comments