19 internal object[] m_constructorArgs;
21 internal byte[] m_blob;
32 InitCustomAttributeBuilder(con, constructorArgs,
new PropertyInfo[0],
new object[0],
new FieldInfo[0],
new object[0]);
45 InitCustomAttributeBuilder(con, constructorArgs, namedProperties, propertyValues,
new FieldInfo[0],
new object[0]);
58 InitCustomAttributeBuilder(con, constructorArgs,
new PropertyInfo[0],
new object[0], namedFields, fieldValues);
73 InitCustomAttributeBuilder(con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
76 private bool ValidateType(
Type t)
107 return t == typeof(
object);
110 internal void InitCustomAttributeBuilder(ConstructorInfo con,
object[] constructorArgs, PropertyInfo[] namedProperties,
object[] propertyValues, FieldInfo[] namedFields,
object[] fieldValues)
114 throw new ArgumentNullException(
"con");
116 if (constructorArgs ==
null)
118 throw new ArgumentNullException(
"constructorArgs");
120 if (namedProperties ==
null)
122 throw new ArgumentNullException(
"namedProperties");
124 if (propertyValues ==
null)
126 throw new ArgumentNullException(
"propertyValues");
128 if (namedFields ==
null)
130 throw new ArgumentNullException(
"namedFields");
132 if (fieldValues ==
null)
134 throw new ArgumentNullException(
"fieldValues");
136 if (namedProperties.Length != propertyValues.Length)
138 throw new ArgumentException(Environment.GetResourceString(
"Arg_ArrayLengthsDiffer"),
"namedProperties, propertyValues");
140 if (namedFields.Length != fieldValues.Length)
142 throw new ArgumentException(Environment.GetResourceString(
"Arg_ArrayLengthsDiffer"),
"namedFields, fieldValues");
146 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadConstructor"));
150 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadConstructorCallConv"));
153 m_constructorArgs =
new object[constructorArgs.Length];
154 Array.Copy(constructorArgs, m_constructorArgs, constructorArgs.Length);
155 Type[] parameterTypes = con.GetParameterTypes();
156 if (parameterTypes.Length != constructorArgs.Length)
158 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadParameterCountsForConstructor"));
160 for (
int i = 0; i < parameterTypes.Length; i++)
162 if (!ValidateType(parameterTypes[i]))
164 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadTypeInCustomAttribute"));
167 for (
int i = 0; i < parameterTypes.Length; i++)
169 if (constructorArgs[i] !=
null)
171 TypeCode typeCode =
Type.GetTypeCode(parameterTypes[i]);
172 if (typeCode !=
Type.GetTypeCode(constructorArgs[i].GetType()) && (typeCode !=
TypeCode.Object || !ValidateType(constructorArgs[i].GetType())))
174 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadParameterTypeForConstructor", i));
180 binaryWriter.
Write((ushort)1);
181 for (
int i = 0; i < constructorArgs.Length; i++)
183 EmitValue(binaryWriter, parameterTypes[i], constructorArgs[i]);
185 binaryWriter.
Write((ushort)(namedProperties.Length + namedFields.Length));
186 for (
int i = 0; i < namedProperties.Length; i++)
188 if (namedProperties[i] ==
null)
190 throw new ArgumentNullException(
"namedProperties[" + i +
"]");
192 Type propertyType = namedProperties[i].PropertyType;
193 if (propertyValues[i] ==
null && propertyType.IsPrimitive)
195 throw new ArgumentNullException(
"propertyValues[" + i +
"]");
197 if (!ValidateType(propertyType))
199 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadTypeInCustomAttribute"));
201 if (!namedProperties[i].CanWrite)
203 throw new ArgumentException(Environment.GetResourceString(
"Argument_NotAWritableProperty"));
205 if (namedProperties[i].DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(namedProperties[i].DeclaringType) && !TypeBuilder.IsTypeEqual(namedProperties[i].DeclaringType, con.DeclaringType) && (!(namedProperties[i].DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)namedProperties[i].DeclaringType).BakedRuntimeType)))
207 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadPropertyForConstructorBuilder"));
209 if (propertyValues[i] !=
null && propertyType != typeof(
object) &&
Type.GetTypeCode(propertyValues[i].GetType()) !=
Type.GetTypeCode(propertyType))
211 throw new ArgumentException(Environment.GetResourceString(
"Argument_ConstantDoesntMatch"));
213 binaryWriter.
Write((
byte)84);
214 EmitType(binaryWriter, propertyType);
215 EmitString(binaryWriter, namedProperties[i].Name);
216 EmitValue(binaryWriter, propertyType, propertyValues[i]);
218 for (
int i = 0; i < namedFields.Length; i++)
220 if (namedFields[i] ==
null)
222 throw new ArgumentNullException(
"namedFields[" + i +
"]");
224 Type fieldType = namedFields[i].FieldType;
225 if (fieldValues[i] ==
null && fieldType.IsPrimitive)
227 throw new ArgumentNullException(
"fieldValues[" + i +
"]");
229 if (!ValidateType(fieldType))
231 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadTypeInCustomAttribute"));
233 if (namedFields[i].DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(namedFields[i].DeclaringType) && !TypeBuilder.IsTypeEqual(namedFields[i].DeclaringType, con.DeclaringType) && (!(namedFields[i].DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType).BakedRuntimeType)))
235 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadFieldForConstructorBuilder"));
237 if (fieldValues[i] !=
null && fieldType != typeof(
object) &&
Type.GetTypeCode(fieldValues[i].GetType()) !=
Type.GetTypeCode(fieldType))
239 throw new ArgumentException(Environment.GetResourceString(
"Argument_ConstantDoesntMatch"));
241 binaryWriter.
Write((
byte)83);
242 EmitType(binaryWriter, fieldType);
243 EmitString(binaryWriter, namedFields[i].Name);
244 EmitValue(binaryWriter, fieldType, fieldValues[i]);
251 if (type.IsPrimitive)
253 switch (
Type.GetTypeCode(type))
256 writer.
Write((
byte)4);
259 writer.
Write((
byte)5);
262 writer.
Write((
byte)3);
265 writer.
Write((
byte)2);
268 writer.
Write((
byte)6);
271 writer.
Write((
byte)7);
274 writer.
Write((
byte)8);
277 writer.
Write((
byte)9);
280 writer.
Write((
byte)10);
283 writer.
Write((
byte)11);
286 writer.
Write((
byte)12);
289 writer.
Write((
byte)13);
293 else if (type.IsEnum)
295 writer.
Write((
byte)85);
296 EmitString(writer, type.AssemblyQualifiedName);
298 else if (type == typeof(
string))
300 writer.
Write((
byte)14);
302 else if (type == typeof(Type))
304 writer.
Write((
byte)80);
306 else if (type.IsArray)
308 writer.
Write((
byte)29);
309 EmitType(writer, type.GetElementType());
313 writer.
Write((
byte)81);
317 private void EmitString(
BinaryWriter writer,
string str)
320 uint num = (uint)bytes.Length;
323 writer.
Write((
byte)num);
325 else if (num <= 16383)
327 writer.
Write((
byte)((num >> 8) | 0x80));
328 writer.
Write((
byte)(num & 0xFF));
332 writer.
Write((
byte)((num >> 24) | 0xC0));
333 writer.
Write((
byte)((num >> 16) & 0xFF));
334 writer.
Write((
byte)((num >> 8) & 0xFF));
335 writer.
Write((
byte)(num & 0xFF));
340 private void EmitValue(
BinaryWriter writer, Type type,
object value)
344 switch (
Type.GetTypeCode(
Enum.GetUnderlyingType(type)))
347 writer.
Write((sbyte)value);
350 writer.
Write((
byte)value);
353 writer.
Write((
short)value);
356 writer.
Write((ushort)value);
359 writer.
Write((
int)value);
362 writer.
Write((uint)value);
365 writer.
Write((
long)value);
368 writer.
Write((ulong)value);
373 if (type == typeof(
string))
377 writer.
Write(
byte.MaxValue);
381 EmitString(writer, (
string)value);
385 if (type == typeof(Type))
389 writer.
Write(
byte.MaxValue);
392 string text = TypeNameBuilder.ToString((Type)value, TypeNameBuilder.Format.AssemblyQualifiedName);
395 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidTypeForCA", value.GetType()));
397 EmitString(writer, text);
404 writer.
Write(uint.MaxValue);
407 Array array = (Array)value;
408 Type elementType = type.GetElementType();
409 writer.
Write(array.Length);
410 for (
int i = 0; i < array.Length; i++)
412 EmitValue(writer, elementType, array.GetValue(i));
416 if (type.IsPrimitive)
418 switch (
Type.GetTypeCode(type))
421 writer.
Write((sbyte)value);
424 writer.
Write((
byte)value);
430 writer.
Write((
byte)(((
bool)value) ? 1 : 0));
433 writer.
Write((
short)value);
436 writer.
Write((ushort)value);
439 writer.
Write((
int)value);
442 writer.
Write((uint)value);
445 writer.
Write((
long)value);
448 writer.
Write((ulong)value);
451 writer.
Write((
float)value);
454 writer.
Write((
double)value);
459 if (type == typeof(
object))
461 Type type2 = (value ==
null) ? typeof(
string) : ((value is
Type) ? typeof(Type) : value.GetType());
462 if (type2 == typeof(
object))
464 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadParameterTypeForCAB", type2.ToString()));
466 EmitType(writer, type2);
467 EmitValue(writer, type2, value);
470 string text2 =
"null";
473 text2 = value.GetType().ToString();
475 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadParameterTypeForCAB", text2));
479 internal void CreateCustomAttribute(ModuleBuilder mod,
int tkOwner)
481 CreateCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con).Token, toDisk:
false);
485 internal int PrepareCreateCustomAttributeToDisk(ModuleBuilder mod)
487 return mod.InternalGetConstructorToken(m_con, usingRef:
true).Token;
491 internal void CreateCustomAttribute(ModuleBuilder mod,
int tkOwner,
int tkAttrib,
bool toDisk)
501 throw new NotImplementedException();
511 throw new NotImplementedException();
523 throw new NotImplementedException();
536 void _CustomAttributeBuilder.
Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid,
short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
538 throw new NotImplementedException();
Represents a character encoding.To browse the .NET Framework source code for this type,...
bool IsPrimitive
Gets a value indicating whether the T:System.Type is one of the primitive types.
MethodAttributes
Specifies flags for method attributes. These flags are defined in the corhdr.h file.
virtual Stream BaseStream
Gets the underlying stream of the T:System.IO.BinaryWriter.
CustomAttributeBuilder(ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues)
Initializes an instance of the CustomAttributeBuilder class given the constructor for the custom attr...
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.
abstract Type GetElementType()
When overridden in a derived class, returns the T:System.Type of the object encompassed or referred t...
virtual byte [] GetBytes(char[] chars)
When overridden in a derived class, encodes all the characters in the specified character array into ...
static TypeCode GetTypeCode(Type type)
Gets the underlying type code of the specified T:System.Type.
TypeCode
Specifies the type of an object.
void GetTypeInfoCount(out uint pcTInfo)
Retrieves the number of type information interfaces that an object provides (either 0 or 1).
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.
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...
virtual bool IsEnum
Gets a value indicating whether the current T:System.Type represents an enumeration.
Helps build custom attributes.
virtual int GetArrayRank()
Gets the number of dimensions in an array.
Modifies code generation for runtime just-in-time (JIT) debugging. This class cannot be inherited.
CallingConventions
Defines the valid calling conventions for a method.
SecurityAction
Specifies the security actions that can be performed using declarative security.
bool IsArray
Gets a value that indicates whether the type is an array.
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
CustomAttributeBuilder(ConstructorInfo con, object[] constructorArgs, FieldInfo[] namedFields, object[] fieldValues)
Initializes an instance of the CustomAttributeBuilder class given the constructor for the custom attr...
Provides the base class for enumerations.
CustomAttributeBuilder(ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues)
Initializes an instance of the CustomAttributeBuilder class given the constructor for the custom attr...
CustomAttributeBuilder(ConstructorInfo con, object[] constructorArgs)
Initializes an instance of the CustomAttributeBuilder class given the constructor for the custom attr...
Represents type declarations: class types, interface types, array types, value types,...
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
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.
Exposes the T:System.Reflection.Emit.CustomAttributeBuilder class to unmanaged code.
Discovers the attributes of a property and provides access to property metadata.
Type DeclaringType
Provides COM objects with version-independent access to the P:System.Reflection.MemberInfo....
Attribute can be applied to an enumeration.
static Encoding UTF8
Gets an encoding for the UTF-8 format.
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
static Type GetUnderlyingType(Type enumType)
Returns the underlying type of the specified enumeration.
Writes primitive types in binary to a stream and supports writing strings in a specific encoding.