typebuilder.cs
来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,316 行 · 第 1/3 页
CS
1,316 行
/* * TypeBuilder.cs - Implementation of the * "System.Reflection.Emit.TypeBuilder" class. * * Copyright (C) 2002 Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */namespace System.Reflection.Emit{#if CONFIG_REFLECTION_EMITusing System;using System.Collections;using System.Globalization;using System.Reflection;using System.Runtime.InteropServices;using System.Runtime.CompilerServices;using System.Security;using System.Security.Permissions;public sealed class TypeBuilder : Type, IClrProgramItem, IDetachItem{ // Internal state. private IntPtr privateData; // Must be the first field. internal ModuleBuilder module; private String name; private String nspace; internal TypeAttributes attr; private Type parent; private Type[] interfaces; private TypeBuilder declaringType; private ClrType type; private Type underlyingSystemType; private ArrayList methods; internal bool needsDefaultConstructor; // Constants. public const int UnspecifiedTypeSize = 0; // Constructor. internal TypeBuilder(ModuleBuilder module, String name, String nspace, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packingSize, int typeSize, TypeBuilder declaringType) { // Validate the parameters. if(name == null) { throw new ArgumentNullException("name"); } else if(name == String.Empty) { throw new ArgumentException(_("Emit_NameEmpty")); } if(nspace == null) { nspace = String.Empty; } // Initialize the internal state. this.module = module; this.name = name; this.nspace = nspace; this.attr = attr; this.parent = parent; this.interfaces = null; this.declaringType = declaringType; this.type = null; this.underlyingSystemType = null; this.methods = new ArrayList(); this.needsDefaultConstructor = true; // We need the AssemblyBuilder lock for the next part. lock(typeof(AssemblyBuilder)) { // Determine the scope to use to declare the type. IntPtr scope; if(declaringType == null) { scope = IntPtr.Zero; } else { scope = ((IClrProgramItem)declaringType).ClrHandle; } // Create the type. privateData = ClrTypeCreate (((IClrProgramItem)module).ClrHandle, scope, name, (nspace == String.Empty ? null : nspace), attr, (parent == null ? new System.Reflection.Emit.TypeToken(0) : module.GetTypeToken(parent))); if(privateData == IntPtr.Zero) { throw new ArgumentException (_("Emit_TypeAlreadyExists")); } module.assembly.AddDetach(this); if(packingSize != PackingSize.Unspecified) { ClrTypeSetPackingSize(privateData, (int)packingSize); } if(typeSize != UnspecifiedTypeSize) { ClrTypeSetClassSize(privateData, typeSize); } } // Add the interfaces to the type. if(interfaces != null) { foreach(Type iface in interfaces) { AddInterfaceImplementation(iface); } } } // Get the attribute flags for this type. protected override TypeAttributes GetAttributeFlagsImpl() { return attr; } // Get the assembly associated with this type. public override Assembly Assembly { get { return module.Assembly; } } // Get the full qualified assembly name of this type. public override String AssemblyQualifiedName { get { return Assembly.CreateQualifiedName (module.Assembly.FullName, name); } } // Get the base type for this type. public override Type BaseType { get { return parent; } } // Get the declaring type for this type. public override Type DeclaringType { get { return declaringType; } } // Get the full name of this type. public override String FullName { get { if(declaringType != null) { return declaringType.FullName + "+" + name; } else if(nspace != null) { return nspace + "." + name; } else { return name; } } } // Get the GUID of this type. public override Guid GUID { get { throw new NotSupportedException(_("NotSupp_Builder")); } } // Retrieve the module that this type is defined within. public override Module Module { get { return module; } } // Get the name of this type. public override String Name { get { return name; } } // Get the namespace of this type. public override String Namespace { get { return nspace; } } // Get the packing size of this type. public PackingSize PackingSize { get { lock(typeof(AssemblyBuilder)) { return (PackingSize) (ClrTypeGetPackingSize(privateData)); } } } // Get the reflected type for this type. public override Type ReflectedType { get { return declaringType; } } // Get the total size of this type. public int Size { get { lock(typeof(AssemblyBuilder)) { return ClrTypeGetClassSize(privateData); } } } // Get the type handle for this type. public override RuntimeTypeHandle TypeHandle { get { throw new NotSupportedException(_("NotSupp_Builder")); } } // Get the type token for this type. public TypeToken TypeToken { get { lock(typeof(AssemblyBuilder)) { return new System.Reflection.Emit.TypeToken (AssemblyBuilder.ClrGetItemToken(privateData)); } } } // Get the underlying system type for this type. public override Type UnderlyingSystemType { get { if(type != null) { return type.UnderlyingSystemType; } else if(IsEnum) { if(underlyingSystemType != null) { return underlyingSystemType; } else { throw new InvalidOperationException (_("Emit_UnderlyingNotSet")); } } return this; } } // Check that the type has not yet been created. internal void CheckNotCreated() { if(type != null) { throw new NotSupportedException(_("NotSupp_TypeCreated")); } } // Check that the type has been created. internal void CheckCreated() { if(type == null) { throw new NotSupportedException (_("NotSupp_TypeNotCreated")); } } // Start a synchronized type builder operation. internal void StartSync() { module.StartSync(); if(type != null) { throw new NotSupportedException(_("NotSupp_TypeCreated")); } } // End a synchronized type builder operation. internal void EndSync() { module.EndSync(); } // Add declarative security to this type. public void AddDeclarativeSecurity(SecurityAction action, PermissionSet pset) { try { StartSync(); module.assembly.AddDeclarativeSecurity (this, action, pset); } finally { EndSync(); } } // Add an interface to this type's implementation list. public void AddInterfaceImplementation(Type interfaceType) { try { // Start builder synchronization. StartSync(); // Validate the parameters. if(interfaceType == null) { throw new ArgumentNullException("interfaceType"); } // Bail out if the supplied parameter is not an interface. // We should probably throw an exception, but MS doesn't. if(!interfaceType.IsInterface) { return; } // Bail out if this type is inherited by the interface // so that we cannot create circular class structures. if(IsAssignableFrom(interfaceType)) { return; } // Determine if we already have this interface. if(interfaces != null) { int index; for(index = 0; index < interfaces.Length; ++index) { if(interfaceType.Equals(interfaces[index])) { return; } } } // Convert the interface into a token, which may throw // an exception if it cannot be imported. TypeToken token = module.GetTypeToken(interfaceType); // Add the interface to the list. Type[] newInterfaces; if(interfaces != null) { newInterfaces = new Type [interfaces.Length + 1]; Array.Copy(interfaces, newInterfaces, interfaces.Length); newInterfaces[interfaces.Length] = interfaceType; } else { newInterfaces = new Type [1]; newInterfaces[0] = interfaceType; } interfaces = newInterfaces; // Call the runtime engine to add the interface. lock(typeof(AssemblyBuilder)) { ClrTypeAddInterface(privateData, token); } } finally { EndSync(); } } // Create this type.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?