c# 动态加载tlb为程序集

private enum RegKind
        {
            RegKind_Default = 0,
            RegKind_Register = 1,
            RegKind_None = 2
        }

        [DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
        private static extern void LoadTypeLibEx(String strTypeLibName, RegKind regKind,
            [MarshalAs(UnmanagedType.Interface)] out Object typeLib);

        public class ConversionEventHandler : ITypeLibImporterNotifySink
        {
            public void ReportEvent(ImporterEventKind eventKind, int eventCode, string eventMsg)
            {
                // handle warning event here...
            }

            public Assembly ResolveRef(object typeLib)
            {
                // resolve reference here and return a correct assembly...
                return null;
            }
        }
/// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Object typeLib;
            LoadTypeLibEx(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb", RegKind.RegKind_None, out typeLib);

            if (typeLib == null)
            {
                Console.WriteLine("LoadTypeLibEx failed.");
                return;
            }

            TypeLibConverter converter = new TypeLibConverter();
            ConversionEventHandler eventHandler = new ConversionEventHandler();
            AssemblyBuilder asm = converter.ConvertTypeLibToAssembly(typeLib, "mscoree.dll", 0, eventHandler, null, null, null, null);
        }

 

posted on 2019-05-17 10:03  空明流光  阅读(995)  评论(0编辑  收藏  举报

导航