Assembly.GetTypes() - ReflectionTypeLoadException

前端 未结 3 1091
生来不讨喜
生来不讨喜 2021-01-01 22:16

We implement a plugin framework for our application and load plugin assemblies using Assembly.Loadfrom. We then use GetTypes() and further examine the types with each plugin

3条回答
  •  一向
    一向 (楼主)
    2021-01-01 22:34

    Thanks to this post I could solve the ReflectionTypeLoadException that I was getting in a UITypeEditor. It's a designer assembly (a winforms smart-tag used at design-time) of a custom class library, that scan for some types.

    /// 
    /// Get the types defined in the RootComponent.
    /// 
    private List getAssemblyTypes(IServiceProvider provider)
    {
        var types = new List();
        try
        {
            IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
            ITypeResolutionService resolution = (ITypeResolutionService)provider.GetService(typeof(ITypeResolutionService));
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                foreach (var assembly in ((AppDomain)sender).GetAssemblies())
                {
                    if (assembly.FullName == args.Name)
                    {
                        return assembly;
                    }
                }
    
                return null;
            };
    
            Type rootComponentType = resolution.GetType(host.RootComponentClassName, false);
            types = rootComponentType.Assembly.GetTypes().ToList();
        }
        catch
        {
        }
    
        return types;
    }
    

提交回复
热议问题