mscorlib(4.0.0.0) API with additions
FieldBuilder.cs
3 using System.Security;
5 
7 {
9  [ClassInterface(ClassInterfaceType.None)]
10  [ComDefaultInterface(typeof(_FieldBuilder))]
11  [ComVisible(true)]
12  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
13  public sealed class FieldBuilder : FieldInfo, _FieldBuilder
14  {
15  private int m_fieldTok;
16 
17  private FieldToken m_tkField;
18 
19  private TypeBuilder m_typeBuilder;
20 
21  private string m_fieldName;
22 
23  private FieldAttributes m_Attributes;
24 
25  private Type m_fieldType;
26 
27  internal int MetadataTokenInternal => m_fieldTok;
28 
31  public override Module Module => m_typeBuilder.Module;
32 
35  public override string Name => m_fieldName;
36 
39  public override Type DeclaringType
40  {
41  get
42  {
43  if (m_typeBuilder.m_isHiddenGlobalType)
44  {
45  return null;
46  }
47  return m_typeBuilder;
48  }
49  }
50 
53  public override Type ReflectedType
54  {
55  get
56  {
57  if (m_typeBuilder.m_isHiddenGlobalType)
58  {
59  return null;
60  }
61  return m_typeBuilder;
62  }
63  }
64 
67  public override Type FieldType => m_fieldType;
68 
72  public override RuntimeFieldHandle FieldHandle
73  {
74  get
75  {
76  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
77  }
78  }
79 
82  public override FieldAttributes Attributes => m_Attributes;
83 
84  [SecurityCritical]
85  internal FieldBuilder(TypeBuilder typeBuilder, string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
86  {
87  if (fieldName == null)
88  {
89  throw new ArgumentNullException("fieldName");
90  }
91  if (fieldName.Length == 0)
92  {
93  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "fieldName");
94  }
95  if (fieldName[0] == '\0')
96  {
97  throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "fieldName");
98  }
99  if (type == null)
100  {
101  throw new ArgumentNullException("type");
102  }
103  if (type == typeof(void))
104  {
105  throw new ArgumentException(Environment.GetResourceString("Argument_BadFieldType"));
106  }
107  m_fieldName = fieldName;
108  m_typeBuilder = typeBuilder;
109  m_fieldType = type;
110  m_Attributes = (attributes & ~FieldAttributes.ReservedMask);
111  SignatureHelper fieldSigHelper = SignatureHelper.GetFieldSigHelper(m_typeBuilder.Module);
112  fieldSigHelper.AddArgument(type, requiredCustomModifiers, optionalCustomModifiers);
113  int length;
114  byte[] signature = fieldSigHelper.InternalGetSignature(out length);
115  m_fieldTok = TypeBuilder.DefineField(m_typeBuilder.GetModuleBuilder().GetNativeHandle(), typeBuilder.TypeToken.Token, fieldName, signature, length, m_Attributes);
116  m_tkField = new FieldToken(m_fieldTok, type);
117  }
118 
119  [SecurityCritical]
120  internal void SetData(byte[] data, int size)
121  {
122  ModuleBuilder.SetFieldRVAContent(m_typeBuilder.GetModuleBuilder().GetNativeHandle(), m_tkField.Token, data, size);
123  }
124 
125  internal TypeBuilder GetTypeBuilder()
126  {
127  return m_typeBuilder;
128  }
129 
134  public override object GetValue(object obj)
135  {
136  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
137  }
138 
146  public override void SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
147  {
148  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
149  }
150 
155  public override object[] GetCustomAttributes(bool inherit)
156  {
157  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
158  }
159 
165  public override object[] GetCustomAttributes(Type attributeType, bool inherit)
166  {
167  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
168  }
169 
176  public override bool IsDefined(Type attributeType, bool inherit)
177  {
178  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
179  }
180 
184  {
185  return m_tkField;
186  }
187 
193  [SecuritySafeCritical]
194  public void SetOffset(int iOffset)
195  {
196  m_typeBuilder.ThrowIfCreated();
197  TypeBuilder.SetFieldLayoutOffset(m_typeBuilder.GetModuleBuilder().GetNativeHandle(), GetToken().Token, iOffset);
198  }
199 
205  [SecuritySafeCritical]
206  [Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
207  public void SetMarshal(UnmanagedMarshal unmanagedMarshal)
208  {
209  if (unmanagedMarshal == null)
210  {
211  throw new ArgumentNullException("unmanagedMarshal");
212  }
213  m_typeBuilder.ThrowIfCreated();
214  byte[] array = unmanagedMarshal.InternalGetBytes();
215  TypeBuilder.SetFieldMarshal(m_typeBuilder.GetModuleBuilder().GetNativeHandle(), GetToken().Token, array, array.Length);
216  }
217 
222  [SecuritySafeCritical]
223  public void SetConstant(object defaultValue)
224  {
225  m_typeBuilder.ThrowIfCreated();
226  TypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), GetToken().Token, m_fieldType, defaultValue);
227  }
228 
235  [SecuritySafeCritical]
236  [ComVisible(true)]
237  public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
238  {
239  if (con == null)
240  {
241  throw new ArgumentNullException("con");
242  }
243  if (binaryAttribute == null)
244  {
245  throw new ArgumentNullException("binaryAttribute");
246  }
247  ModuleBuilder moduleBuilder = m_typeBuilder.Module as ModuleBuilder;
248  m_typeBuilder.ThrowIfCreated();
249  TypeBuilder.DefineCustomAttribute(moduleBuilder, m_tkField.Token, moduleBuilder.GetConstructorToken(con).Token, binaryAttribute, toDisk: false, updateCompilerFlags: false);
250  }
251 
257  [SecuritySafeCritical]
258  public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
259  {
260  if (customBuilder == null)
261  {
262  throw new ArgumentNullException("customBuilder");
263  }
264  m_typeBuilder.ThrowIfCreated();
265  ModuleBuilder mod = m_typeBuilder.Module as ModuleBuilder;
266  customBuilder.CreateCustomAttribute(mod, m_tkField.Token);
267  }
268 
272  void _FieldBuilder.GetTypeInfoCount(out uint pcTInfo)
273  {
274  throw new NotImplementedException();
275  }
276 
282  void _FieldBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
283  {
284  throw new NotImplementedException();
285  }
286 
294  void _FieldBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
295  {
296  throw new NotImplementedException();
297  }
298 
309  void _FieldBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
310  {
311  throw new NotImplementedException();
312  }
313  }
314 }
void AddArgument(Type clsArgument)
Adds an argument to the signature.
Performs reflection on a module.
Definition: Module.cs:17
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static SignatureHelper GetFieldSigHelper(Module mod)
Returns a signature helper for a field.
void SetCustomAttribute(CustomAttributeBuilder customBuilder)
Sets a custom attribute using a custom attribute builder.
FieldToken GetToken()
Returns the token representing this field.
Discovers the attributes of a class constructor and provides access to constructor metadata.
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
Represents a field using an internal metadata token.
void SetMarshal(UnmanagedMarshal unmanagedMarshal)
Describes the native marshaling of the field.
Provides methods for building signatures.
void GetTypeInfoCount(out uint pcTInfo)
Retrieves the number of type information interfaces that an object provides (either 0 or 1).
override Type ReflectedType
Indicates the reference to the T:System.Type object from which this object was obtained....
Definition: FieldBuilder.cs:54
MethodToken GetConstructorToken(ConstructorInfo constructor, IEnumerable< Type > optionalParameterTypes)
Returns the token used to identify the constructor that has the specified attributes and parameter ty...
Defines and represents a field. This class cannot be inherited.
Definition: FieldBuilder.cs:13
void GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
Retrieves the type information for an object, which can be used to get the type information for an in...
override string Name
Indicates the name of this field. This property is read-only.
Definition: FieldBuilder.cs:35
override void SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
Sets the value of the field supported by the given object.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
Defines and creates new instances of classes during run time.
Definition: TypeBuilder.cs:15
override Module Module
Retrieves the dynamic module that contains this type definition.
Definition: TypeBuilder.cs:133
Defines and represents a module in a dynamic assembly.
FieldAttributes
Specifies flags that describe the attributes of a field.
TypeToken TypeToken
Returns the type token of this type.
Definition: TypeBuilder.cs:308
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
void GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
Maps a set of names to a corresponding set of dispatch identifiers.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
void Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
Provides access to properties and methods exposed by an object.
override object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns all the custom attributes defined for this field identified by the given type.
Selects a member from a list of candidates, and performs type conversion from actual argument type to...
Definition: Binder.cs:10
Exposes the T:System.Reflection.Emit.FieldBuilder class to unmanaged code.
override Type DeclaringType
Indicates a reference to the T:System.Type object for the type that declares this field....
Definition: FieldBuilder.cs:40
override bool IsDefined(Type attributeType, bool inherit)
Indicates whether an attribute having the specified type is defined on a field.
The exception that is thrown when one of the arguments provided to a method is not valid.
Represents the class that describes how to marshal a field from managed to unmanaged code....
void SetConstant(object defaultValue)
Sets the default value of this field.
int Token
Returns the metadata token for this method.
Definition: MethodToken.cs:17
override RuntimeFieldHandle FieldHandle
Indicates the internal metadata handle for this field. This property is read-only.
Definition: FieldBuilder.cs:73
override Type FieldType
Indicates the T:System.Type object that represents the type of this field. This property is read-only...
Definition: FieldBuilder.cs:67
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
int Token
Retrieves the metadata token for this class.
Definition: TypeToken.cs:17
override object GetValue(object obj)
Retrieves the value of the field supported by the given object.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
Sets a custom attribute using a specified custom attribute blob.
The exception that is thrown when a requested method or operation is not implemented.
int Token
Retrieves the metadata token for this field.
Definition: FieldToken.cs:19
override FieldAttributes Attributes
Indicates the attributes of this field. This property is read-only.
Definition: FieldBuilder.cs:82
override object [] GetCustomAttributes(bool inherit)
Returns all the custom attributes defined for this field.
void SetOffset(int iOffset)
Specifies the field layout.
The FieldToken struct is an object representation of a token that represents a field.
Definition: FieldToken.cs:8