mscorlib(4.0.0.0) API with additions
MarshalAsAttribute.cs
1 using System.Reflection;
2 using System.Security;
3 
5 {
7  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public sealed class MarshalAsAttribute : Attribute
11  {
12  internal UnmanagedType _val;
13 
15  [__DynamicallyInvokable]
17 
19  [__DynamicallyInvokable]
21 
23  [__DynamicallyInvokable]
24  public int IidParameterIndex;
25 
27  [__DynamicallyInvokable]
29 
31  [__DynamicallyInvokable]
32  public short SizeParamIndex;
33 
35  [__DynamicallyInvokable]
36  public int SizeConst;
37 
39  [ComVisible(true)]
40  [__DynamicallyInvokable]
41  public string MarshalType;
42 
44  [ComVisible(true)]
45  [__DynamicallyInvokable]
47 
49  [__DynamicallyInvokable]
50  public string MarshalCookie;
51 
54  [__DynamicallyInvokable]
55  public UnmanagedType Value
56  {
57  [__DynamicallyInvokable]
58  get
59  {
60  return _val;
61  }
62  }
63 
64  [SecurityCritical]
65  internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
66  {
67  return GetCustomAttribute(parameter.MetadataToken, parameter.GetRuntimeModule());
68  }
69 
70  [SecurityCritical]
71  internal static bool IsDefined(RuntimeParameterInfo parameter)
72  {
73  return GetCustomAttribute(parameter) != null;
74  }
75 
76  [SecurityCritical]
77  internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
78  {
79  return GetCustomAttribute(field.MetadataToken, field.GetRuntimeModule());
80  }
81 
82  [SecurityCritical]
83  internal static bool IsDefined(RuntimeFieldInfo field)
84  {
85  return GetCustomAttribute(field) != null;
86  }
87 
88  [SecurityCritical]
89  internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
90  {
91  int sizeParamIndex = 0;
92  int sizeConst = 0;
93  string marshalType = null;
94  string marshalCookie = null;
95  string safeArrayUserDefinedSubType = null;
96  int iidParamIndex = 0;
97  ConstArray fieldMarshal = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);
98  if (fieldMarshal.Length == 0)
99  {
100  return null;
101  }
102  MetadataImport.GetMarshalAs(fieldMarshal, out UnmanagedType unmanagedType, out VarEnum safeArraySubType, out safeArrayUserDefinedSubType, out UnmanagedType arraySubType, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie, out iidParamIndex);
103  RuntimeType safeArrayUserDefinedSubType2 = (safeArrayUserDefinedSubType == null || safeArrayUserDefinedSubType.Length == 0) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedSubType, scope);
104  RuntimeType marshalTypeRef = null;
105  try
106  {
107  marshalTypeRef = ((marshalType == null) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalType, scope));
108  }
109  catch (TypeLoadException)
110  {
111  }
112  return new MarshalAsAttribute(unmanagedType, safeArraySubType, safeArrayUserDefinedSubType2, arraySubType, (short)sizeParamIndex, sizeConst, marshalType, marshalTypeRef, marshalCookie, iidParamIndex);
113  }
114 
115  internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType, short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
116  {
117  _val = val;
118  SafeArraySubType = safeArraySubType;
119  SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
120  IidParameterIndex = iidParamIndex;
121  ArraySubType = arraySubType;
122  SizeParamIndex = sizeParamIndex;
123  SizeConst = sizeConst;
124  MarshalType = marshalType;
125  MarshalTypeRef = marshalTypeRef;
126  MarshalCookie = marshalCookie;
127  }
128 
131  [__DynamicallyInvokable]
132  public MarshalAsAttribute(UnmanagedType unmanagedType)
133  {
134  _val = unmanagedType;
135  }
136 
139  [__DynamicallyInvokable]
140  public MarshalAsAttribute(short unmanagedType)
141  {
142  _val = (UnmanagedType)unmanagedType;
143  }
144  }
145 }
string MarshalCookie
Provides additional information to a custom marshaler.
short SizeParamIndex
Indicates the zero-based parameter that contains the count of array elements, similar to size_is in C...
VarEnum SafeArraySubType
Indicates the element type of the F:System.Runtime.InteropServices.UnmanagedType.SafeArray.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
Definition: __Canon.cs:3
Type MarshalTypeRef
Implements F:System.Runtime.InteropServices.MarshalAsAttribute.MarshalType as a type.
MarshalAsAttribute(UnmanagedType unmanagedType)
Initializes a new instance of the T:System.Runtime.InteropServices.MarshalAsAttribute class with the ...
MarshalAsAttribute(short unmanagedType)
Initializes a new instance of the T:System.Runtime.InteropServices.MarshalAsAttribute class with the ...
Indicates how to marshal the data between managed and unmanaged code.
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
int SizeConst
Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in...
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Type SafeArrayUserDefinedSubType
Indicates the user-defined element type of the F:System.Runtime.InteropServices.UnmanagedType....
AttributeTargets
Specifies the application elements on which it is valid to apply an attribute.
UnmanagedType Value
Gets the T:System.Runtime.InteropServices.UnmanagedType value the data is to be marshaled as.
VarEnum
Indicates how to marshal the array elements when an array is marshaled from managed to unmanaged code...
Definition: VarEnum.cs:7
UnmanagedType ArraySubType
Specifies the element type of the unmanaged F:System.Runtime.InteropServices.UnmanagedType....
Attribute()
Initializes a new instance of the T:System.Attribute class.
Definition: Attribute.cs:1016
int IidParameterIndex
Specifies the parameter index of the unmanaged iid_is attribute used by COM.
string MarshalType
Specifies the fully qualified name of a custom marshaler.