typebuilder.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,316 行 · 第 1/3 页

CS
1,316
字号
	public Type CreateType()			{				try				{					// Synchronize access and make sure we aren't created.					StartSync();					// If nested, the nesting parent must be created first.					if(declaringType != null)					{						if(declaringType.type == null)						{							throw new InvalidOperationException								(_("Emit_NestingParentNotCreated"));						}					}					// Define a default constructor if necessary.					if(needsDefaultConstructor && !IsInterface && !IsValueType)					{						if(IsAbstract)						{							DefineDefaultConstructor(MethodAttributes.Family);						}						else						{							DefineDefaultConstructor(MethodAttributes.Public);						}					}					// Finalize the methods and constructors.					MethodBuilder mb;					foreach(MethodBase method in methods)					{						mb = (method as MethodBuilder);						if(mb != null)						{							mb.FinalizeMethod();						}						else						{							((ConstructorBuilder)method).FinalizeConstructor();						}					}					// Wrap "privateData" in a "ClrType" object and return it.					lock(typeof(AssemblyBuilder))					{						if(privateData == IntPtr.Zero)						{							throw new InvalidOperationException								(_("Emit_TypeInvalid"));						}						ClrType clrType = new ClrType();						clrType.privateData = privateData;						type = clrType;						return type;					}				}				finally				{					EndSync();				}			}	// Define a constructor for this class.	public ConstructorBuilder DefineConstructor				(MethodAttributes attributes,				 CallingConventions callingConvention,				 Type[] parameterTypes)			{				try				{					StartSync();					String name;					if((attributes & MethodAttributes.Static) != 0)					{						name = ".cctor";					}					else					{						name = ".ctor";					}					attributes |= MethodAttributes.SpecialName;					ConstructorBuilder con = new ConstructorBuilder						(this, name, attributes,						 callingConvention, parameterTypes);					needsDefaultConstructor = false;					return con;				}				finally				{					EndSync();				}			}	// Define a default constructor for this class.	public ConstructorBuilder DefineDefaultConstructor				(MethodAttributes attributes)			{				return DefineConstructor(attributes,										 CallingConventions.Standard,										 null);			}	// Define an event for this class.	public EventBuilder DefineEvent				(String name, EventAttributes attributes, Type eventType)			{				try				{					StartSync();					return new EventBuilder(this, name, attributes, eventType);				}				finally				{					EndSync();				}			}	// Define a field for this class.	public FieldBuilder DefineField				(String name, Type type, FieldAttributes attributes)			{				try				{					StartSync();					if(IsEnum && underlyingSystemType == null &&					   (attributes & FieldAttributes.Static) == 0)					{						underlyingSystemType = type;					}					return new FieldBuilder(this, name, type, attributes);				}				finally				{					EndSync();				}			}	// Define a data field within this class.	private FieldBuilder DefineData(String name, byte[] data,								    int size, FieldAttributes attributes)			{				// Validate the parameters.				if(name == null)				{					throw new ArgumentNullException("name");				}				else if(name == String.Empty)				{					throw new ArgumentException(_("Emit_NameEmpty"));				}				else if(size <= 0 || size > 0x003EFFFF)				{					throw new ArgumentException(_("Emit_DataSize"));				}				// We must not have created the type yet.				CheckNotCreated();				// Look for or create a value type for the field.				String typeName = "$ArrayType$" + size.ToString();				Type type = module.GetType(typeName);				if(type == null)				{					TypeBuilder builder;					builder = module.DefineType							(typeName,							 TypeAttributes.Public |							 TypeAttributes.Sealed |							 TypeAttributes.ExplicitLayout,							 typeof(System.ValueType),							 PackingSize.Size1, size);					type = builder.CreateType();				}				// Define the field and set the data on it.				FieldBuilder field = DefineField					(name, type, attributes | FieldAttributes.Static);				field.SetData(data, size);				return field;			}	// Define static initialized data within this class.	public FieldBuilder DefineInitializedData				(String name, byte[] data, FieldAttributes attributes)			{				try				{					StartSync();					if(data == null)					{						throw new ArgumentNullException("data");					}					return DefineData(name, data, data.Length, attributes);				}				finally				{					EndSync();				}			}	// Define a method for this class.	public MethodBuilder DefineMethod				(String name, MethodAttributes attributes,				 CallingConventions callingConvention,				 Type returnType, Type[] parameterTypes)			{				try				{					StartSync();					return new MethodBuilder						(this, name, attributes, callingConvention,						 returnType, parameterTypes);				}				finally				{					EndSync();				}			}	public MethodBuilder DefineMethod				(String name, MethodAttributes attributes,				 Type returnType, Type[] parameterTypes)			{				return DefineMethod(name, attributes,									CallingConventions.Standard,									returnType, parameterTypes);			}	// Define a method override declaration for this class.	public void DefineMethodOverride				(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)			{				try				{					StartSync();										// Validate the parameters.					if(methodInfoBody == null)					{						throw new ArgumentNullException("methodInfoBody");					}					if(methodInfoDeclaration == null)					{						throw new ArgumentNullException							("methodInfoDeclaration");					}					if(methodInfoBody.DeclaringType != this)					{						throw new ArgumentException							(_("Emit_OverrideBodyNotInType"));					}					MethodToken bodyToken = module.GetMethodToken						(methodInfoBody);					MethodToken declToken = module.GetMethodToken						(methodInfoDeclaration);					lock(typeof(AssemblyBuilder))					{						ClrTypeAddOverride							(module.privateData,							 bodyToken.Token, declToken.Token);					}				}				finally				{					EndSync();				}			}	// Define a nested type within this class.	private TypeBuilder DefineNestedType				(String name, TypeAttributes attr, Type parent,				 Type[] interfaces, int typeSize, PackingSize packingSize)			{				try				{					StartSync();					return new TypeBuilder(module,										   name, null, attr, parent,										   interfaces, packingSize,										   typeSize, this);				}				finally				{					EndSync();				}			}	public TypeBuilder DefineNestedType(String name)			{				return DefineNestedType(name,									    TypeAttributes.NestedPrivate,										null, null, 0,										PackingSize.Unspecified);			}	public TypeBuilder DefineNestedType(String name, TypeAttributes attr)			{				return DefineNestedType(name, attr, null, null, 0,										PackingSize.Unspecified);			}	public TypeBuilder DefineNestedType(String name, TypeAttributes attr,										Type parent)			{				return DefineNestedType(name, attr, parent, null, 0,										PackingSize.Unspecified);			}	public TypeBuilder DefineNestedType(String name, TypeAttributes attr,										Type parent, int typeSize)			{				return DefineNestedType(name, attr, parent, null, typeSize,										PackingSize.Unspecified);			}	public TypeBuilder DefineNestedType(String name, TypeAttributes attr,										Type parent, PackingSize packSize)			{				return DefineNestedType(name, attr, parent, null, 0, packSize);			}	public TypeBuilder DefineNestedType(String name, TypeAttributes attr,										Type parent, Type[] interfaces)			{				return DefineNestedType(name, attr, parent, interfaces,										0, PackingSize.Unspecified);			}	// Define a PInvoke method for this class.	public MethodBuilder DefinePInvokeMethod				(String name, String dllName, String entryName,				 MethodAttributes attributes,				 CallingConventions callingConvention,				 Type returnType, Type[] parameterTypes,				 CallingConvention nativeCallConv,				 CharSet nativeCharSet)			{				try				{					// Lock down the assembly while we do this.					StartSync();					// Validate the parameters.					if(name == null)					{						throw new ArgumentNullException("name");					}					if(name == String.Empty)					{						throw new ArgumentException(_("Emit_NameEmpty"));					}					if(dllName == null)					{						throw new ArgumentNullException("dllName");					}					if(dllName == String.Empty)					{						throw new ArgumentException(_("Emit_NameEmpty"));					}					if(entryName == null)					{						throw new ArgumentNullException("entryName");					}					if(entryName == String.Empty)					{						throw new ArgumentException(_("Emit_NameEmpty"));					}					if((type.Attributes & TypeAttributes.ClassSemanticsMask)							== TypeAttributes.Interface)					{						throw new ArgumentException							(_("Emit_PInvokeInInterface"));					}					if((attributes & MethodAttributes.Abstract) != 0)					{						throw new ArgumentException							(_("Emit_PInvokeAbstract"));					}					// Create the underlying method.					MethodBuilder method = new MethodBuilder							(this, name,							 attributes | MethodAttributes.PinvokeImpl,							 callingConvention, returnType, parameterTypes);					// Build the attributes for the PInvoke declaration.					int pinvAttrs = (((int)nativeCallConv) << 8);					switch(nativeCharSet)					{						case CharSet.Ansi:		pinvAttrs |= 0x0002; break;						case CharSet.Unicode:	pinvAttrs |= 0x0004; break;						case CharSet.Auto:		pinvAttrs |= 0x0006; break;						default:				break;					}					// Create the PInvoke declaration on the method.					if(entryName == name)					{						entryName = null;					}					lock(typeof(AssemblyBuilder))					{						MethodBuilder.ClrMethodAddPInvoke							(((IClrProgramItem)method).ClrHandle,							 pinvAttrs, dllName, entryName);					}					// Return the method to the caller.					return method;				}				finally				{					EndSync();				}			}	public MethodBuilder DefinePInvokeMethod				(String name, String dllName,				 MethodAttributes attributes,				 CallingConventions callingConvention,				 Type returnType, Type[] parameterTypes,				 CallingConvention nativeCallConv,				 CharSet nativeCharSet)			{				return DefinePInvokeMethod(name, dllName, name,										   attributes, callingConvention,										   returnType, parameterTypes,										   nativeCallConv, nativeCharSet);			}	// Define a property for this class.	public PropertyBuilder DefineProperty				(String name, PropertyAttributes attributes,				 Type returnType, Type[] parameterTypes)			{				try				{					StartSync();					return new PropertyBuilder						(this, name, attributes, returnType, parameterTypes);				}				finally				{					EndSync();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?