public AssemblyBuilder ConvertTypeLibToAssembly([MarshalAs(UnmanagedType.Interface)] object typeLib, string asmFileName, TypeLibImporterFlags flags, ITypeLibImporterNotifySink notifySink, byte[] publicKey, StrongNameKeyPair keyPair, string asmNamespace, Version asmVersion)
        {
            if (typeLib == null)
            {
                throw new ArgumentNullException("typeLib");
            }
            if (asmFileName == null)
            {
                throw new ArgumentNullException("asmFileName");
            }
            if (notifySink == null)
            {
                throw new ArgumentNullException("notifySink");
            }
            if (string.Empty.Equals(asmFileName))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileName"), "asmFileName");
            }
            if (asmFileName.Length > 260)
            {
                throw new ArgumentException(Environment.GetResourceString("IO.PathTooLong"), asmFileName);
            }
            if ((((flags & TypeLibImporterFlags.PrimaryInteropAssembly) != TypeLibImporterFlags.None) && (publicKey == null)) && (keyPair == null))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
            }
            ArrayList         eventItfInfoList = null;
            AssemblyNameFlags none             = AssemblyNameFlags.None;
            AssemblyName      asmName          = GetAssemblyNameFromTypelib(typeLib, asmFileName, publicKey, keyPair, asmVersion, none);
            AssemblyBuilder   asmBldr          = CreateAssemblyForTypeLib(typeLib, asmFileName, asmName, (flags & TypeLibImporterFlags.PrimaryInteropAssembly) != TypeLibImporterFlags.None, (flags & TypeLibImporterFlags.ReflectionOnlyLoading) != TypeLibImporterFlags.None, (flags & TypeLibImporterFlags.NoDefineVersionResource) != TypeLibImporterFlags.None);
            string            fileName         = Path.GetFileName(asmFileName);
            ModuleBuilder     mod = asmBldr.DefineDynamicModule(fileName, fileName);

            if (asmNamespace == null)
            {
                asmNamespace = asmName.Name;
            }
            TypeResolveHandler  handler  = new TypeResolveHandler(mod, notifySink);
            AppDomain           domain   = Thread.GetDomain();
            ResolveEventHandler handler2 = new ResolveEventHandler(handler.ResolveEvent);
            ResolveEventHandler handler3 = new ResolveEventHandler(handler.ResolveAsmEvent);
            ResolveEventHandler handler4 = new ResolveEventHandler(handler.ResolveROAsmEvent);

            domain.TypeResolve     += handler2;
            domain.AssemblyResolve += handler3;
            domain.ReflectionOnlyAssemblyResolve += handler4;
            nConvertTypeLibToMetadata(typeLib, asmBldr.InternalAssembly, mod.InternalModule, asmNamespace, flags, handler, out eventItfInfoList);
            UpdateComTypesInAssembly(asmBldr, mod);
            if (eventItfInfoList.Count > 0)
            {
                new TCEAdapterGenerator().Process(mod, eventItfInfoList);
            }
            domain.TypeResolve     -= handler2;
            domain.AssemblyResolve -= handler3;
            domain.ReflectionOnlyAssemblyResolve -= handler4;
            return(asmBldr);
        }
 public AssemblyBuilder ConvertTypeLibToAssembly([MarshalAs(UnmanagedType.Interface)] object typeLib, string asmFileName, TypeLibImporterFlags flags, ITypeLibImporterNotifySink notifySink, byte[] publicKey, StrongNameKeyPair keyPair, string asmNamespace, Version asmVersion)
 {
     if (typeLib == null)
     {
         throw new ArgumentNullException("typeLib");
     }
     if (asmFileName == null)
     {
         throw new ArgumentNullException("asmFileName");
     }
     if (notifySink == null)
     {
         throw new ArgumentNullException("notifySink");
     }
     if (string.Empty.Equals(asmFileName))
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileName"), "asmFileName");
     }
     if (asmFileName.Length > 260)
     {
         throw new ArgumentException(Environment.GetResourceString("IO.PathTooLong"), asmFileName);
     }
     if ((((flags & TypeLibImporterFlags.PrimaryInteropAssembly) != TypeLibImporterFlags.None) && (publicKey == null)) && (keyPair == null))
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
     }
     ArrayList eventItfInfoList = null;
     AssemblyNameFlags none = AssemblyNameFlags.None;
     AssemblyName asmName = GetAssemblyNameFromTypelib(typeLib, asmFileName, publicKey, keyPair, asmVersion, none);
     AssemblyBuilder asmBldr = CreateAssemblyForTypeLib(typeLib, asmFileName, asmName, (flags & TypeLibImporterFlags.PrimaryInteropAssembly) != TypeLibImporterFlags.None, (flags & TypeLibImporterFlags.ReflectionOnlyLoading) != TypeLibImporterFlags.None, (flags & TypeLibImporterFlags.NoDefineVersionResource) != TypeLibImporterFlags.None);
     string fileName = Path.GetFileName(asmFileName);
     ModuleBuilder mod = asmBldr.DefineDynamicModule(fileName, fileName);
     if (asmNamespace == null)
     {
         asmNamespace = asmName.Name;
     }
     TypeResolveHandler handler = new TypeResolveHandler(mod, notifySink);
     AppDomain domain = Thread.GetDomain();
     ResolveEventHandler handler2 = new ResolveEventHandler(handler.ResolveEvent);
     ResolveEventHandler handler3 = new ResolveEventHandler(handler.ResolveAsmEvent);
     ResolveEventHandler handler4 = new ResolveEventHandler(handler.ResolveROAsmEvent);
     domain.TypeResolve += handler2;
     domain.AssemblyResolve += handler3;
     domain.ReflectionOnlyAssemblyResolve += handler4;
     nConvertTypeLibToMetadata(typeLib, asmBldr.InternalAssembly, mod.InternalModule, asmNamespace, flags, handler, out eventItfInfoList);
     UpdateComTypesInAssembly(asmBldr, mod);
     if (eventItfInfoList.Count > 0)
     {
         new TCEAdapterGenerator().Process(mod, eventItfInfoList);
     }
     domain.TypeResolve -= handler2;
     domain.AssemblyResolve -= handler3;
     domain.ReflectionOnlyAssemblyResolve -= handler4;
     return asmBldr;
 }
        public AssemblyBuilder ConvertTypeLibToAssembly([MarshalAs(UnmanagedType.Interface)] Object typeLib, 
                                                        String asmFileName,
                                                        TypeLibImporterFlags flags, 
                                                        ITypeLibImporterNotifySink notifySink,
                                                        byte[] publicKey,
                                                        StrongNameKeyPair keyPair,
                                                        String asmNamespace,
                                                        Version asmVersion)
        {
            // Validate the arguments.
            if (typeLib == null)
                throw new ArgumentNullException("typeLib");
            if (asmFileName == null)
                throw new ArgumentNullException("asmFileName");         
            if (notifySink == null)
                throw new ArgumentNullException("notifySink");
            if (String.Empty.Equals(asmFileName))
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileName"), "asmFileName");
            if (asmFileName.Length > Path.MaxPath)
                throw new ArgumentException(Environment.GetResourceString("IO.PathTooLong"), asmFileName);
            if ((flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0 && publicKey == null && keyPair == null)
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
            Contract.EndContractBlock();

            ArrayList eventItfInfoList = null;

            // Determine the AssemblyNameFlags
            AssemblyNameFlags asmNameFlags = AssemblyNameFlags.None;
            
            // Retrieve the assembly name from the typelib.
            AssemblyName asmName = GetAssemblyNameFromTypelib(typeLib, asmFileName, publicKey, keyPair, asmVersion, asmNameFlags);

            // Create the dynamic assembly that will contain the converted typelib types.
            AssemblyBuilder asmBldr = CreateAssemblyForTypeLib(typeLib, asmFileName, asmName, 
                                        (flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0,
                                        (flags & TypeLibImporterFlags.ReflectionOnlyLoading) != 0,
                                        (flags & TypeLibImporterFlags.NoDefineVersionResource) != 0);

            // Define a dynamic module that will contain the contain the imported types.
            String strNonQualifiedAsmFileName = Path.GetFileName(asmFileName);
            ModuleBuilder modBldr = asmBldr.DefineDynamicModule(strNonQualifiedAsmFileName, strNonQualifiedAsmFileName);

            // If the namespace hasn't been specified, then use the assembly name.
            if (asmNamespace == null)
                asmNamespace = asmName.Name;

            // Create a type resolve handler that will also intercept resolve ref messages
            // on the sink interface to build up a list of referenced assemblies.
            TypeResolveHandler typeResolveHandler = new TypeResolveHandler(modBldr, notifySink);

            // Add a listener for the type resolve events.
            AppDomain currentDomain = Thread.GetDomain();
            ResolveEventHandler resolveHandler = new ResolveEventHandler(typeResolveHandler.ResolveEvent);
            ResolveEventHandler asmResolveHandler = new ResolveEventHandler(typeResolveHandler.ResolveAsmEvent);
            ResolveEventHandler ROAsmResolveHandler = new ResolveEventHandler(typeResolveHandler.ResolveROAsmEvent);
            currentDomain.TypeResolve += resolveHandler;
            currentDomain.AssemblyResolve += asmResolveHandler;
            currentDomain.ReflectionOnlyAssemblyResolve += ROAsmResolveHandler;

            // Convert the types contained in the typelib into metadata and add them to the assembly.
            nConvertTypeLibToMetadata(typeLib, asmBldr.InternalAssembly, modBldr.InternalModule, asmNamespace, flags, typeResolveHandler, out eventItfInfoList);

            // Update the COM types in the assembly.
            UpdateComTypesInAssembly(asmBldr, modBldr);

            // If there are any event sources then generate the TCE adapters.
            if (eventItfInfoList.Count > 0)
                new TCEAdapterGenerator().Process(modBldr, eventItfInfoList);

            // Remove the listener for the type resolve events.
            currentDomain.TypeResolve -= resolveHandler;
            currentDomain.AssemblyResolve -= asmResolveHandler;
            currentDomain.ReflectionOnlyAssemblyResolve -= ROAsmResolveHandler;

            // We have finished converting the typelib and now have a fully formed assembly.
            return asmBldr;
        }
    public static void Main(String[] args)
    {
        AssemblyName asmName = new AssemblyName();

        asmName.Name = "NestedEnum";
        AssemblyBuilder asmBuild = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder   modBuild = asmBuild.DefineDynamicModule("ModuleOne", "NestedEnum.dll");

        // Hook up the event listening.
        TypeResolveHandler typeResolveHandler = new TypeResolveHandler(modBuild);
        // Add a listener for the type resolve events.
        AppDomain           currentDomain  = Thread.GetDomain();
        ResolveEventHandler resolveHandler = new ResolveEventHandler(typeResolveHandler.ResolveEvent);

        currentDomain.TypeResolve += resolveHandler;

        TypeBuilder tb = modBuild.DefineType("AType", TypeAttributes.Public);
        TypeBuilder eb = tb.DefineNestedType("AnEnum", TypeAttributes.NestedPublic | TypeAttributes.Sealed, typeof(Enum), null);

        eb.DefineField("value__", typeof(int), FieldAttributes.Private | FieldAttributes.SpecialName);
        FieldBuilder fb = eb.DefineField("Field1", eb, FieldAttributes.Public | FieldAttributes.Literal | FieldAttributes.Static);

        fb.SetConstant(1);

        enumType = eb;

        // Comment out this field.
        // When this field is defined, the loader cannot determine the size
        // of the type. Therefore, a TypeResolve event is generated when the
        // nested type is completed.
        tb.DefineField("Field2", eb, FieldAttributes.Public);

        tNesting = tb.CreateType();
        if (tNesting == null)
        {
            Console.WriteLine("NestingType CreateType failed but didn't throw!");
        }

        try {
            tNested = eb.CreateType();
            if (tNested == null)
            {
                Console.WriteLine("NestedType CreateType failed but didn't throw!");
            }
        }
        catch {
            // This is needed because you might have already completed the type in the TypeResolve event.
        }

        if (tNested != null)
        {
            Type x = tNested.DeclaringType;
            if (x == null)
            {
                Console.WriteLine("Declaring type was null.");
            }
            else
            {
                Console.WriteLine(x.Name);
            }
        }

        asmBuild.Save("NestedEnum.dll");

        // Remove the listener for the type resolve events.
        currentDomain.TypeResolve -= resolveHandler;
    }
Esempio n. 5
0
        public AssemblyBuilder ConvertTypeLibToAssembly([MarshalAs(UnmanagedType.Interface)] Object typeLib,
                                                        String asmFileName,
                                                        TypeLibImporterFlags flags,
                                                        ITypeLibImporterNotifySink notifySink,
                                                        byte[] publicKey,
                                                        StrongNameKeyPair keyPair,
                                                        String asmNamespace,
                                                        Version asmVersion)
        {
            // Validate the arguments.
            if (typeLib == null)
            {
                throw new ArgumentNullException("typeLib");
            }
            if (asmFileName == null)
            {
                throw new ArgumentNullException("asmFileName");
            }
            if (notifySink == null)
            {
                throw new ArgumentNullException("notifySink");
            }
            if (String.Empty.Equals(asmFileName))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileName"), "asmFileName");
            }
            if (asmFileName.Length > Path.MAX_PATH)
            {
                throw new ArgumentException(Environment.GetResourceString("IO.PathTooLong"), asmFileName);
            }
            if ((flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0 && publicKey == null && keyPair == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
            }
            Contract.EndContractBlock();

            ArrayList eventItfInfoList = null;

            // Determine the AssemblyNameFlags
            AssemblyNameFlags asmNameFlags = AssemblyNameFlags.None;

            // Retrieve the assembly name from the typelib.
            AssemblyName asmName = GetAssemblyNameFromTypelib(typeLib, asmFileName, publicKey, keyPair, asmVersion, asmNameFlags);

            // Create the dynamic assembly that will contain the converted typelib types.
            AssemblyBuilder asmBldr = CreateAssemblyForTypeLib(typeLib, asmFileName, asmName,
                                                               (flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0,
                                                               (flags & TypeLibImporterFlags.ReflectionOnlyLoading) != 0,
                                                               (flags & TypeLibImporterFlags.NoDefineVersionResource) != 0);

            // Define a dynamic module that will contain the contain the imported types.
            String        strNonQualifiedAsmFileName = Path.GetFileName(asmFileName);
            ModuleBuilder modBldr = asmBldr.DefineDynamicModule(strNonQualifiedAsmFileName, strNonQualifiedAsmFileName);

            // If the namespace hasn't been specified, then use the assembly name.
            if (asmNamespace == null)
            {
                asmNamespace = asmName.Name;
            }

            // Create a type resolve handler that will also intercept resolve ref messages
            // on the sink interface to build up a list of referenced assemblies.
            TypeResolveHandler typeResolveHandler = new TypeResolveHandler(modBldr, notifySink);

            // Add a listener for the type resolve events.
            AppDomain           currentDomain       = Thread.GetDomain();
            ResolveEventHandler resolveHandler      = new ResolveEventHandler(typeResolveHandler.ResolveEvent);
            ResolveEventHandler asmResolveHandler   = new ResolveEventHandler(typeResolveHandler.ResolveAsmEvent);
            ResolveEventHandler ROAsmResolveHandler = new ResolveEventHandler(typeResolveHandler.ResolveROAsmEvent);

            currentDomain.TypeResolve     += resolveHandler;
            currentDomain.AssemblyResolve += asmResolveHandler;
            currentDomain.ReflectionOnlyAssemblyResolve += ROAsmResolveHandler;

            // Convert the types contained in the typelib into metadata and add them to the assembly.
            nConvertTypeLibToMetadata(typeLib, asmBldr.InternalAssembly, modBldr.InternalModule, asmNamespace, flags, typeResolveHandler, out eventItfInfoList);

            // Update the COM types in the assembly.
            UpdateComTypesInAssembly(asmBldr, modBldr);

            // If there are any event sources then generate the TCE adapters.
            if (eventItfInfoList.Count > 0)
            {
                new TCEAdapterGenerator().Process(modBldr, eventItfInfoList);
            }

            // Remove the listener for the type resolve events.
            currentDomain.TypeResolve     -= resolveHandler;
            currentDomain.AssemblyResolve -= asmResolveHandler;
            currentDomain.ReflectionOnlyAssemblyResolve -= ROAsmResolveHandler;

            // We have finished converting the typelib and now have a fully formed assembly.
            return(asmBldr);
        }