/// <summary>Checks if the given object is an instance of TypeToken and is equal to this instance.</summary> /// <returns>true if <paramref name="obj" /> is an instance of TypeToken and is equal to this object; otherwise, false.</returns> /// <param name="obj">The object to compare with this TypeToken. </param> public override bool Equals(object obj) { bool flag = obj is TypeToken; if (flag) { TypeToken typeToken = (TypeToken)obj; flag = (this.tokValue == typeToken.tokValue); } return(flag); }
/// <summary> /// </summary> public override bool Equals(object obj) { bool res = obj is TypeToken; if (res) { TypeToken that = (TypeToken)obj; res = (this.tokValue == that.tokValue); } return(res); }
//************************************************ // // Add a type token into signature. CorType will be either ELEMENT_TYPE_CLASS or ELEMENT_TYPE_VALUECLASS // //************************************************ private void InternalAddTypeToken(TypeToken clsToken, int CorType) { AddElementType(CorType); AddToken(clsToken.Token); }
extern private static void ClrTypeSetParent (IntPtr classInfo, TypeToken parent);
public bool Equals(TypeToken obj) { return(this.tokValue == obj.tokValue); }
private void InternalAddTypeToken(TypeToken clsToken, int CorType) { // Add a type token into signature. CorType will be either ELEMENT_TYPE_CLASS or ELEMENT_TYPE_VALUECLASS AddElementType(CorType); AddToken(clsToken.Token); }
public bool Equals(TypeToken obj) { return obj.m_class == m_class; }
/// <summary>Indicates whether the current instance is equal to the specified <see cref="T:System.Reflection.Emit.TypeToken" />.</summary><returns>true if the value of <paramref name="obj" /> is equal to the value of the current instance; otherwise, false.</returns><param name="obj">The <see cref="T:System.Reflection.Emit.TypeToken" /> to compare to the current instance.</param> public bool Equals(TypeToken obj) { throw new NotImplementedException(); }
private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType) { // Add a type token into signature. CorType will be either CorElementType.ELEMENT_TYPE_CLASS or CorElementType.ELEMENT_TYPE_VALUETYPE AddElementType(CorType); AddToken(clsToken.Token); }
extern private static void ClrTypeAddInterface (IntPtr classInfo, TypeToken iface);
private TypeToken GetTypeTokenNoLock(Type type) { CheckContext(type); // Return a token for the class relative to the Module. Tokens // are used to indentify objects when the objects are used in IL // instructions. Tokens are always relative to the Module. For example, // the token value for System.String is likely to be different from // Module to Module. Calling GetTypeToken will cause a reference to be // added to the Module. This reference becomes a perminate part of the Module, // multiple calles to this method with the same class have no additional side affects. // This function is optimized to use the TypeDef token if Type is within the same module. // We should also be aware of multiple dynamic modules and multiple implementation of Type!!! TypeToken tkToken; bool isSameAssembly; bool isSameModule; Module refedModule; String strRefedModuleFileName = String.Empty; // assume that referenced module is non-transient. Only if the referenced module is dynamic, // and transient, this variable will be set to true. bool isRefedModuleTransient = false; if (type == null) throw new ArgumentNullException("type"); refedModule = type.Module; isSameModule = refedModule.Equals(this); if (type.IsByRef) throw new ArgumentException(Environment.GetResourceString("Argument_CannotGetTypeTokenForByRef")); if ((type.IsGenericType && !type.IsGenericTypeDefinition) || type.IsGenericParameter || type.IsArray || type.IsPointer) { int length; byte[] sig = SignatureHelper.GetTypeSigToken(this, type).InternalGetSignature(out length); return new TypeToken(InternalGetTypeSpecTokenWithBytes(sig, length)); } // After this point, it is not an array type nor Pointer type if (isSameModule) { // no need to do anything additional other than defining the TypeRef Token if (type is TypeBuilder) { // optimization: if the type is defined in this module, // just return the token // TypeBuilder typeBuilder = (TypeBuilder) type; return typeBuilder.TypeToken; } else if (type is EnumBuilder) { TypeBuilder typeBuilder = ((EnumBuilder) type).m_typeBuilder; return typeBuilder.TypeToken; } else if (type is GenericTypeParameterBuilder) { return new TypeToken(type.MetadataTokenInternal); } return new TypeToken(GetTypeRefNested(type, this, String.Empty)); } // After this point, the referenced module is not the same as the referencing // module. // isSameAssembly = refedModule.Assembly.Equals(Assembly); if (refedModule is ModuleBuilder) { ModuleBuilder refedModuleBuilder = (ModuleBuilder) refedModule; if (refedModuleBuilder.IsTransient()) { isRefedModuleTransient = true; } // get the referenced module's file name strRefedModuleFileName = refedModuleBuilder.m_moduleData.m_strFileName; } else strRefedModuleFileName = refedModule.ScopeName; // We cannot have a non-transient module referencing to a transient module. if (IsTransient() == false && isRefedModuleTransient) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadTransientModuleReference")); } tkToken = new TypeToken(GetTypeRefNested(type, refedModule, strRefedModuleFileName)); return tkToken; }
extern private static IntPtr ClrTypeCreate (IntPtr module, IntPtr nestedParent, String name, String nspace, TypeAttributes attr, TypeToken parent);
// Used to construct the place holder for global functions and data members internal TypeBuilder( ModuleBuilder module) // module containing this type { // set the token to be the null TypeDef token m_tdType = new TypeToken(SignatureHelper.mdtTypeDef); m_isHiddenGlobalType = true; m_module = (ModuleBuilder) module; m_listMethods = new ArrayList(); }
// 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(); } }
private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType) { this.AddElementType(CorType); this.AddToken(clsToken.Token); }
// ctor for the global (module) type internal TypeBuilder(ModuleBuilder module) { m_tdType = new TypeToken((int)MetadataTokenType.TypeDef); m_isHiddenGlobalType = true; m_module = (ModuleBuilder)module; m_listMethods = new List<MethodBuilder>(); // No token has been created so let's initialize it to -1 // The first time we call MethodBuilder.GetToken this will incremented. m_lastTokenizedMethod = -1; }
/// <summary> /// Emits the typeof(Type) /// </summary> /// <param name="generator">The generator.</param> /// <param name="type">The type.</param> public static void EmitTypeOf(this ILGenerator generator, TypeToken type) { generator.Emit(OpCodes.Ldtoken, type.Token); var tTypeMeth = typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }); generator.Emit(OpCodes.Call, tTypeMeth); }
public bool Equals(TypeToken obj) { return default(bool); }
public bool Equals(TypeToken obj) { return(default(bool)); }
private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType) { // Add a type token into signature. CorType will be either CorElementType.Class or CorElementType.ValueType AddElementType(CorType); AddToken(clsToken.Token); }
private Type CreateTypeNoLock() { if (IsCreated()) return m_runtimeType; ThrowIfGeneric(); ThrowIfCreated(); if (m_typeInterfaces == null) m_typeInterfaces = new Type[0]; int[] interfaceTokens = new int[m_typeInterfaces.Length]; for(int i = 0; i < m_typeInterfaces.Length; i++) { interfaceTokens[i] = m_module.GetTypeToken(m_typeInterfaces[i]).Token; } int tkParent = 0; if (m_typeParent != null) tkParent = m_module.GetTypeToken(m_typeParent).Token; if (IsGenericParameter) { int[] constraints = new int[m_typeInterfaces.Length]; if (m_typeParent != null) { constraints = new int[m_typeInterfaces.Length + 1]; constraints[constraints.Length - 1] = tkParent; } for(int i = 0; i < m_typeInterfaces.Length; i++) { constraints[i] = m_module.GetTypeToken(m_typeInterfaces[i]).Token; } int declMember = m_declMeth == null ? m_DeclaringType.m_tdType.Token : m_declMeth.GetToken().Token; m_tdType = new TypeToken(InternalDefineGenParam( m_strName, declMember, m_genParamPos, (int)m_genParamAttributes, constraints, m_module, 0)); if (m_ca != null) { foreach (CustAttr ca in m_ca) ca.Bake(m_module, MetadataTokenInternal); } m_hasBeenCreated = true; return this; } else { // Check for global typebuilder if (((m_tdType.Token & 0x00FFFFFF) != 0) && ((tkParent & 0x00FFFFFF) != 0)) InternalSetParentType(m_tdType.Token, tkParent, m_module); if (m_inst != null) foreach (Type tb in m_inst) if (tb is GenericTypeParameterBuilder) ((GenericTypeParameterBuilder)tb).m_type.CreateType(); } byte [] body; MethodAttributes methodAttrs; int maxstack; if (!m_isHiddenGlobalType) { // create a public default constructor if this class has no constructor. // except if the type is Interface, ValueType, Enum, or a static class. if (m_constructorCount == 0 && ((m_iAttr & TypeAttributes.Interface) == 0) && !IsValueType && ((m_iAttr & (TypeAttributes.Abstract | TypeAttributes.Sealed)) != (TypeAttributes.Abstract | TypeAttributes.Sealed))) { DefineDefaultConstructor(MethodAttributes.Public); } } int size = m_listMethods.Count; for(int i = 0; i < size; i++) { MethodBuilder meth =(MethodBuilder)m_listMethods[i]; if (meth.IsGenericMethodDefinition) meth.GetToken(); // Doubles as "CreateMethod" for MethodBuilder -- analagous to CreateType() methodAttrs = meth.Attributes; // Any of these flags in the implemenation flags is set, we will not attach the IL method body if (((meth.GetMethodImplementationFlags() &(MethodImplAttributes.CodeTypeMask|MethodImplAttributes.PreserveSig|MethodImplAttributes.Unmanaged)) != MethodImplAttributes.IL) || ((methodAttrs & MethodAttributes.PinvokeImpl) !=(MethodAttributes) 0)) { continue; } int sigLength; byte[] LocalSig= meth.GetLocalsSignature().InternalGetSignature(out sigLength); // Check that they haven't declared an abstract method on a non-abstract class if (((methodAttrs & MethodAttributes.Abstract) != 0) &&((m_iAttr & TypeAttributes.Abstract) == 0)) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadTypeAttributesNotAbstract")); } body = meth.GetBody(); // If this is an abstract method or an interface, we don't need to set the IL. if ((methodAttrs & MethodAttributes.Abstract) != 0) { // We won't check on Interface because we can have class static initializer on interface. // We will just let EE or validator to catch the problem. //((m_iAttr & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface)) if (body != null) throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadMethodBody")); } else if (body == null || body.Length == 0) { // If it's not an abstract or an interface, set the IL. if (meth.m_ilGenerator != null) { // we need to bake the method here. meth.CreateMethodBodyHelper(meth.GetILGenerator()); } body = meth.GetBody(); if ((body == null || body.Length == 0) && !meth.m_canBeRuntimeImpl) throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody"), meth.Name) ); } if (meth.m_ilGenerator != null) { maxstack = meth.m_ilGenerator.GetMaxStackSize(); } else { // this is the case when client provide an array of IL byte stream rather than going through ILGenerator. maxstack = 16; } __ExceptionInstance[] Exceptions = meth.GetExceptionInstances(); int[] TokenFixups = meth.GetTokenFixups(); int[] RVAFixups = meth.GetRVAFixups(); __ExceptionInstance[] localExceptions = null; int[] localTokenFixups = null; int[] localRVAFixups = null; if (Exceptions != null) { localExceptions = new __ExceptionInstance[Exceptions.Length]; Array.Copy(Exceptions, localExceptions, Exceptions.Length); } if (TokenFixups != null) { localTokenFixups = new int[TokenFixups.Length]; Array.Copy(TokenFixups, localTokenFixups, TokenFixups.Length); } if (RVAFixups != null) { localRVAFixups = new int[RVAFixups.Length]; Array.Copy(RVAFixups, localRVAFixups, RVAFixups.Length); } InternalSetMethodIL(meth.GetToken().Token, meth.InitLocals, body, LocalSig, sigLength, maxstack, meth.GetNumberOfExceptions(), localExceptions, localTokenFixups, localRVAFixups, m_module); } m_hasBeenCreated = true; // Terminate the process. Type cls = TermCreateClass(m_tdType.Token, m_module); if (!m_isHiddenGlobalType) { m_runtimeType =(RuntimeType) cls; // if this type is a nested type, we need to invalidate the cached nested runtime type on the nesting type if (m_DeclaringType != null &&(RuntimeType)m_DeclaringType.m_runtimeType != null) { ((RuntimeType)m_DeclaringType.m_runtimeType).InvalidateCachedNestedType(); } return cls; } else { return null; } }
// Return a token for the class relative to the Module. Tokens // are used to indentify objects when the objects are used in IL // instructions. Tokens are always relative to the Module. For example, // the token value for System.String is likely to be different from // Module to Module. Calling GetTypeToken will cause a reference to be // added to the Module. This reference becomes a perminate part of the Module, // multiple calles to this method with the same class have no additional side affects. // // This function is optimized to use the TypeDef token if Type is within the same module. // We should also be aware of multiple dynamic modules and multiple implementation of Type!!! /// <include file='doc\ModuleBuilder.uex' path='docs/doc[@for="ModuleBuilder.GetTypeToken"]/*' /> public TypeToken GetTypeToken(Type type) { try { Enter(); TypeToken tkToken; bool isSameAssembly; bool isSameModule; Module refedModule; String strRefedModuleFileName = String.Empty; // assume that referenced module is non-transient. Only if the referenced module is dynamic, // and transient, this variable will be set to true. bool isRefedModuleTransient = false; if (type == null) throw new ArgumentNullException("type"); BCLDebug.Log("DYNIL","## DYNIL LOGGING: ModuleBuilder.GetTypeToken( " + type.FullName + " )"); refedModule = type.Module; isSameModule = refedModule.Equals(this); // Console.WriteLine("***** " + type.FullName); if (type.IsByRef) { // cannot be by ref!!! Throw exception throw new ArgumentException(Environment.GetResourceString("Argument_CannotGetTypeTokenForByRef")); } if (type is SymbolType) { if (type.IsPointer || type.IsArray) { SignatureBuffer sigBuf = new SignatureBuffer(); SymbolType symType = (SymbolType) type; // convert SymbolType to blob form symType.ToSigBytes(this, sigBuf); return new TypeToken( InternalGetTypeSpecTokenWithBytes(sigBuf.m_buf, sigBuf.m_curPos) ); } else throw new ArgumentException(Environment.GetResourceString("Argument_UnknownTypeForGetTypeToken")); } Type baseType; baseType = GetBaseType(type); if ( (type.IsArray || type.IsPointer) && baseType != type) { // We will at most recursive once. int baseToken; baseToken = GetTypeToken(baseType).Token; if (!(type is RuntimeType)) throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); return new TypeToken( InternalGetTypeSpecToken((RuntimeType)type, baseToken) ); } // After this point, it is not an array type nor Pointer type if (isSameModule) { // no need to do anything additional other than defining the TypeRef Token if (type is TypeBuilder) { // optimization: if the type is defined in this module, // just return the token // TypeBuilder typeBuilder = (TypeBuilder) type; return typeBuilder.TypeToken; } else if (type is EnumBuilder) { TypeBuilder typeBuilder = ((EnumBuilder) type).m_typeBuilder; return typeBuilder.TypeToken; } return new TypeToken(GetTypeRefNested(type, this, String.Empty)); } // After this point, the referenced module is not the same as the referencing // module. // isSameAssembly = refedModule.Assembly.Equals(Assembly); if (refedModule is ModuleBuilder) { ModuleBuilder refedModuleBuilder = (ModuleBuilder) refedModule; if (refedModuleBuilder.IsTransient()) { isRefedModuleTransient = true; } // get the referenced module's file name strRefedModuleFileName = refedModuleBuilder.m_moduleData.m_strFileName; } else strRefedModuleFileName = refedModule.ScopeName; // We cannot have a non-transient module referencing to a transient module. if (IsTransient() == false && isRefedModuleTransient) { // we got problem here. throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadTransientModuleReference")); } tkToken = new TypeToken(GetTypeRefNested(type, refedModule, strRefedModuleFileName)); return tkToken; } finally { Exit(); } }
private TypeBuilder(TypeBuilder genTypeDef, GenericTypeParameterBuilder[] inst) { m_genTypeDef = genTypeDef; m_DeclaringType = genTypeDef.m_DeclaringType; m_typeParent = genTypeDef.m_typeParent; m_runtimeType = genTypeDef.m_runtimeType; m_tdType = genTypeDef.m_tdType; m_strName = genTypeDef.m_strName; m_bIsGenParam = false; m_bIsGenTypeDef = false; m_module = genTypeDef.m_module; m_inst = inst; m_hasBeenCreated = true; }
public bool Equals(TypeToken obj) { return(obj.m_class == m_class); }
internal TypeBuilder(ModuleBuilder module) { m_tdType = new TypeToken(SignatureHelper.mdtTypeDef); m_isHiddenGlobalType = true; m_module =(ModuleBuilder)module; m_listMethods = new ArrayList(); }
private void Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, Module module, PackingSize iPackingSize, int iTypeSize, TypeBuilder enclosingType) { int i; int[] interfaceTokens; m_bIsGenTypeDef = false; interfaceTokens = null; m_bIsGenParam = false; m_hasBeenCreated = false; m_runtimeType = null; m_isHiddenGlobalType = false; m_isHiddenType = false; m_module =(ModuleBuilder) module; m_DeclaringType = enclosingType; Assembly containingAssem = m_module.Assembly; m_underlyingSystemType = null; // used when client use TypeBuilder to define Enum if (fullname == null) throw new ArgumentNullException("fullname"); if (fullname.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "fullname"); if (fullname[0] == '\0') throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "fullname"); if (fullname.Length > 1023) throw new ArgumentException(Environment.GetResourceString("Argument_TypeNameTooLong"), "fullname"); // cannot have two types within the same assembly of the same name containingAssem.m_assemblyData.CheckTypeNameConflict(fullname, enclosingType); if (enclosingType != null) { // Nested Type should have nested attribute set. // If we are renumbering TypeAttributes' bit, we need to change the logic here. if (((attr & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||((attr & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic)) throw new ArgumentException(Environment.GetResourceString("Argument_BadNestedTypeFlags"), "attr"); } if (interfaces != null) { for(i = 0; i < interfaces.Length; i++) { if (interfaces[i] == null) { // cannot contain null in the interface list throw new ArgumentNullException("interfaces"); } } interfaceTokens = new int[interfaces.Length]; for(i = 0; i < interfaces.Length; i++) { interfaceTokens[i] = m_module.GetTypeToken(interfaces[i]).Token; } } int iLast = fullname.LastIndexOf('.'); if (iLast == -1 || iLast == 0) { // no name space m_strNameSpace = String.Empty; m_strName = fullname; } else { // split the name space m_strNameSpace = fullname.Substring(0, iLast); m_strName = fullname.Substring(iLast + 1); } VerifyTypeAttributes(attr); m_iAttr = attr; SetParent(parent); m_listMethods = new ArrayList(); SetInterfaces(interfaces); m_constructorCount=0; int tkParent = 0; if (m_typeParent != null) tkParent = m_module.GetTypeToken(m_typeParent).Token; int tkEnclosingType = 0; if (enclosingType != null) { tkEnclosingType = enclosingType.m_tdType.Token; } m_tdType = new TypeToken(InternalDefineClass( fullname, tkParent, interfaceTokens, m_iAttr, m_module, Guid.Empty, tkEnclosingType, 0)); m_iPackingSize = iPackingSize; m_iTypeSize = iTypeSize; if ((m_iPackingSize != 0) ||(m_iTypeSize != 0)) InternalSetClassLayout(Module, m_tdType.Token, m_iPackingSize, m_iTypeSize); // If the type is public and it is contained in a assemblyBuilder, // update the public COMType list. if (IsPublicComType(this)) { if (containingAssem is AssemblyBuilder) { AssemblyBuilder assemBuilder =(AssemblyBuilder) containingAssem; if (assemBuilder.IsPersistable() && m_module.IsTransient() == false) { assemBuilder.m_assemblyData.AddPublicComType(this); } } } }
// // Summary: // Indicates whether the current instance is equal to the specified System.Reflection.Emit.TypeToken. // // Parameters: // obj: // The System.Reflection.Emit.TypeToken to compare to the current instance. // // Returns: // true if the value of obj is equal to the value of the current instance; otherwise, // false. public bool Equals(TypeToken obj);
public bool Equals(TypeToken obj) { return (obj.m_class == this.m_class); }
private void Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, ModuleBuilder module, PackingSize iPackingSize, int iTypeSize, TypeBuilder enclosingType) { if (fullname == null) throw new ArgumentNullException(nameof(fullname)); if (fullname.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), nameof(fullname)); if (fullname[0] == '\0') throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), nameof(fullname)); if (fullname.Length > 1023) throw new ArgumentException(Environment.GetResourceString("Argument_TypeNameTooLong"), nameof(fullname)); Contract.EndContractBlock(); int i; m_module = module; m_DeclaringType = enclosingType; AssemblyBuilder containingAssem = m_module.ContainingAssemblyBuilder; // cannot have two types within the same assembly of the same name containingAssem.m_assemblyData.CheckTypeNameConflict(fullname, enclosingType); if (enclosingType != null) { // Nested Type should have nested attribute set. // If we are renumbering TypeAttributes' bit, we need to change the logic here. if (((attr & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||((attr & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic)) throw new ArgumentException(Environment.GetResourceString("Argument_BadNestedTypeFlags"), nameof(attr)); } int[] interfaceTokens = null; if (interfaces != null) { for(i = 0; i < interfaces.Length; i++) { if (interfaces[i] == null) { // cannot contain null in the interface list throw new ArgumentNullException(nameof(interfaces)); } } interfaceTokens = new int[interfaces.Length + 1]; for(i = 0; i < interfaces.Length; i++) { interfaceTokens[i] = m_module.GetTypeTokenInternal(interfaces[i]).Token; } } int iLast = fullname.LastIndexOf('.'); if (iLast == -1 || iLast == 0) { // no name space m_strNameSpace = String.Empty; m_strName = fullname; } else { // split the name space m_strNameSpace = fullname.Substring(0, iLast); m_strName = fullname.Substring(iLast + 1); } VerifyTypeAttributes(attr); m_iAttr = attr; SetParent(parent); m_listMethods = new List<MethodBuilder>(); m_lastTokenizedMethod = -1; SetInterfaces(interfaces); int tkParent = 0; if (m_typeParent != null) tkParent = m_module.GetTypeTokenInternal(m_typeParent).Token; int tkEnclosingType = 0; if (enclosingType != null) { tkEnclosingType = enclosingType.m_tdType.Token; } m_tdType = new TypeToken(DefineType(m_module.GetNativeHandle(), fullname, tkParent, m_iAttr, tkEnclosingType, interfaceTokens)); m_iPackingSize = iPackingSize; m_iTypeSize = iTypeSize; if ((m_iPackingSize != 0) ||(m_iTypeSize != 0)) SetClassLayout(GetModuleBuilder().GetNativeHandle(), m_tdType.Token, m_iPackingSize, m_iTypeSize); m_module.AddType(FullName, this); }
static TypeToken () { Empty = new TypeToken (); }
[System.Security.SecurityCritical] // auto-generated private TypeInfo CreateTypeNoLock() { if (IsCreated()) return m_bakedRuntimeType; ThrowIfCreated(); if (m_typeInterfaces == null) m_typeInterfaces = new List<Type>(); int[] interfaceTokens = new int[m_typeInterfaces.Count]; for(int i = 0; i < m_typeInterfaces.Count; i++) { interfaceTokens[i] = m_module.GetTypeTokenInternal(m_typeInterfaces[i]).Token; } int tkParent = 0; if (m_typeParent != null) tkParent = m_module.GetTypeTokenInternal(m_typeParent).Token; if (IsGenericParameter) { int[] constraints; // Array of token constrains terminated by null token if (m_typeParent != null) { constraints = new int[m_typeInterfaces.Count + 2]; constraints[constraints.Length - 2] = tkParent; } else { constraints = new int[m_typeInterfaces.Count + 1]; } for (int i = 0; i < m_typeInterfaces.Count; i++) { constraints[i] = m_module.GetTypeTokenInternal(m_typeInterfaces[i]).Token; } int declMember = m_declMeth == null ? m_DeclaringType.m_tdType.Token : m_declMeth.GetToken().Token; m_tdType = new TypeToken(DefineGenericParam(m_module.GetNativeHandle(), m_strName, declMember, m_genParamAttributes, m_genParamPos, constraints)); if (m_ca != null) { foreach (CustAttr ca in m_ca) ca.Bake(m_module, MetadataTokenInternal); } m_hasBeenCreated = true; // Baking a generic parameter does not put sufficient information into the metadata to actually be able to load it as a type, // the associated generic type/method needs to be baked first. So we return this rather than the baked type. return this; } else { // Check for global typebuilder if (((m_tdType.Token & 0x00FFFFFF) != 0) && ((tkParent & 0x00FFFFFF) != 0)) SetParentType(m_module.GetNativeHandle(), m_tdType.Token, tkParent); if (m_inst != null) foreach (Type tb in m_inst) if (tb is GenericTypeParameterBuilder) ((GenericTypeParameterBuilder)tb).m_type.CreateType(); } byte [] body; MethodAttributes methodAttrs; if (!m_isHiddenGlobalType) { // create a public default constructor if this class has no constructor. // except if the type is Interface, ValueType, Enum, or a static class. if (m_constructorCount == 0 && ((m_iAttr & TypeAttributes.Interface) == 0) && !IsValueType && ((m_iAttr & (TypeAttributes.Abstract | TypeAttributes.Sealed)) != (TypeAttributes.Abstract | TypeAttributes.Sealed))) { DefineDefaultConstructor(MethodAttributes.Public); } } int size = m_listMethods.Count; for(int i = 0; i < size; i++) { MethodBuilder meth = m_listMethods[i]; if (meth.IsGenericMethodDefinition) meth.GetToken(); // Doubles as "CreateMethod" for MethodBuilder -- analagous to CreateType() methodAttrs = meth.Attributes; // Any of these flags in the implemenation flags is set, we will not attach the IL method body if (((meth.GetMethodImplementationFlags() &(MethodImplAttributes.CodeTypeMask|MethodImplAttributes.PreserveSig|MethodImplAttributes.Unmanaged)) != MethodImplAttributes.IL) || ((methodAttrs & MethodAttributes.PinvokeImpl) !=(MethodAttributes) 0)) { continue; } int sigLength; byte[] localSig = meth.GetLocalSignature(out sigLength); // Check that they haven't declared an abstract method on a non-abstract class if (((methodAttrs & MethodAttributes.Abstract) != 0) &&((m_iAttr & TypeAttributes.Abstract) == 0)) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadTypeAttributesNotAbstract")); } body = meth.GetBody(); // If this is an abstract method or an interface, we don't need to set the IL. if ((methodAttrs & MethodAttributes.Abstract) != 0) { // We won't check on Interface because we can have class static initializer on interface. // We will just let EE or validator to catch the problem. //((m_iAttr & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface)) if (body != null) throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadMethodBody")); } else if (body == null || body.Length == 0) { // If it's not an abstract or an interface, set the IL. if (meth.m_ilGenerator != null) { // we need to bake the method here. meth.CreateMethodBodyHelper(meth.GetILGenerator()); } body = meth.GetBody(); if ((body == null || body.Length == 0) && !meth.m_canBeRuntimeImpl) throw new InvalidOperationException( Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody", meth.Name) ); } int maxStack = meth.GetMaxStack(); ExceptionHandler[] exceptions = meth.GetExceptionHandlers(); int[] tokenFixups = meth.GetTokenFixups(); SetMethodIL(m_module.GetNativeHandle(), meth.GetToken().Token, meth.InitLocals, body, (body != null) ? body.Length : 0, localSig, sigLength, maxStack, exceptions, (exceptions != null) ? exceptions.Length : 0, tokenFixups, (tokenFixups != null) ? tokenFixups.Length : 0); if (m_module.ContainingAssemblyBuilder.m_assemblyData.m_access == AssemblyBuilderAccess.Run) { // if we don't need the data structures to build the method any more // throw them away. meth.ReleaseBakedStructures(); } } m_hasBeenCreated = true; // Terminate the process. RuntimeType cls = null; TermCreateClass(m_module.GetNativeHandle(), m_tdType.Token, JitHelpers.GetObjectHandleOnStack(ref cls)); if (!m_isHiddenGlobalType) { m_bakedRuntimeType = cls; // if this type is a nested type, we need to invalidate the cached nested runtime type on the nesting type if (m_DeclaringType != null && m_DeclaringType.m_bakedRuntimeType != null) { m_DeclaringType.m_bakedRuntimeType.InvalidateCachedNestedType(); } return cls; } else { return null; } }
public bool Equals (TypeToken obj) { return (this.tokValue == obj.tokValue); }
[System.Security.SecurityCritical] // auto-generated private void Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, ModuleBuilder module, PackingSize iPackingSize, int iTypeSize, TypeBuilder enclosingType) { if (fullname == null) throw new ArgumentNullException("fullname"); if (fullname.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "fullname"); if (fullname[0] == '\0') throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "fullname"); if (fullname.Length > 1023) throw new ArgumentException(Environment.GetResourceString("Argument_TypeNameTooLong"), "fullname"); Contract.EndContractBlock(); int i; m_module = module; m_DeclaringType = enclosingType; AssemblyBuilder containingAssem = m_module.ContainingAssemblyBuilder; // cannot have two types within the same assembly of the same name containingAssem.m_assemblyData.CheckTypeNameConflict(fullname, enclosingType); if (enclosingType != null) { // Nested Type should have nested attribute set. // If we are renumbering TypeAttributes' bit, we need to change the logic here. if (((attr & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||((attr & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic)) throw new ArgumentException(Environment.GetResourceString("Argument_BadNestedTypeFlags"), "attr"); } int[] interfaceTokens = null; if (interfaces != null) { for(i = 0; i < interfaces.Length; i++) { if (interfaces[i] == null) { // cannot contain null in the interface list throw new ArgumentNullException("interfaces"); } } interfaceTokens = new int[interfaces.Length + 1]; for(i = 0; i < interfaces.Length; i++) { interfaceTokens[i] = m_module.GetTypeTokenInternal(interfaces[i]).Token; } } int iLast = fullname.LastIndexOf('.'); if (iLast == -1 || iLast == 0) { // no name space m_strNameSpace = String.Empty; m_strName = fullname; } else { // split the name space m_strNameSpace = fullname.Substring(0, iLast); m_strName = fullname.Substring(iLast + 1); } VerifyTypeAttributes(attr); m_iAttr = attr; SetParent(parent); m_listMethods = new List<MethodBuilder>(); m_lastTokenizedMethod = -1; SetInterfaces(interfaces); int tkParent = 0; if (m_typeParent != null) tkParent = m_module.GetTypeTokenInternal(m_typeParent).Token; int tkEnclosingType = 0; if (enclosingType != null) { tkEnclosingType = enclosingType.m_tdType.Token; } m_tdType = new TypeToken(DefineType(m_module.GetNativeHandle(), fullname, tkParent, m_iAttr, tkEnclosingType, interfaceTokens)); m_iPackingSize = iPackingSize; m_iTypeSize = iTypeSize; if ((m_iPackingSize != 0) ||(m_iTypeSize != 0)) SetClassLayout(GetModuleBuilder().GetNativeHandle(), m_tdType.Token, m_iPackingSize, m_iTypeSize); #if !FEATURE_CORECLR // If the type is public and it is contained in a assemblyBuilder, // update the public COMType list. if (IsPublicComType(this)) { if (containingAssem.IsPersistable() && m_module.IsTransient() == false) { // This will throw InvalidOperationException if the assembly has been saved // Ideally we should reject all emit operations if the assembly has been saved, // but that would be a breaking change for some. Currently you cannot define // modules and public types, but you can still define private types and global methods. containingAssem.m_assemblyData.AddPublicComType(this); } // Now add the type to the ExportedType table if (!m_module.Equals(containingAssem.ManifestModule)) containingAssem.DefineExportedTypeInMemory(this, m_module.m_moduleData.FileToken, m_tdType.Token); } #endif m_module.AddType(FullName, this); }
static TypeToken() { Empty = new TypeToken(); }