• C#关于反射加载的问题


    三个程序集: 
    主程序集:BaseApp.exe 
    接口程序集:IBaseApplication 
    插件程序集:TestAttri 
    ======================================================================================= 
    在接口程序中: 
    接口:IApp 
    属性定义:ModuleAttribute 

    复制代码代码如下:

    public interface IApp : IMothed 

    void ParentForm(IApp frm); 

    namespace IBaseApplication.Attributes 

    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = false)] 
    public class ModuleAttribute : Attribute 

    public string IdName { get; set; } 
    public string ModuleName { get; set; } 
    public Type ModuleType { get; set; } 
    //public string AsmName { get; set; } 
    //public string ClassName { get; set; } 
    public string Description { get; set; } 


    在插件程序集中: 
    在该插件程序集中的AssemblyInfo类中标识如下 

    复制代码代码如下:

    [assembly: IBaseApplication.Attributes.Module(ModuleType = typeof(UserControl1), IdName = "be4d9a5b-0455-4e9d-a255-25122b80bef1-UserControl1", ModuleName = "UserControl1", Description = "")] 
    [assembly: IBaseApplication.Attributes.Module(ModuleType = typeof(UserControl2), IdName = "be4d9a5b-0455-4e9d-a255-25122b80bef1-UserControl2", ModuleName = "UserControl2", Description = "")] 


    有两个模块分别是以下 

    复制代码代码如下:

    namespace TestAttri 

    public partial class UserControl1 : UserControl, IApp 

    …… 


    namespace TestAttri 

    public partial class UserControl2 : UserControl, IApp 

    …… 


    ================================================================================================= 
    在主程序集中: 
    将插件放至到:Application.StartupPath + "\Plus" 
    引用了接口程序集“IBaseApplication” 

    复制代码代码如下:

    /// <summary> 
    /// 获取插件文件名称 
    /// </summary> 
    /// <returns></returns> 
    public string[] GetPlusFiles() 

    return System.IO.Directory.GetFiles(Application.StartupPath + "\Plus"); 

    /// <summary> 
    /// 加载插件 
    /// </summary> 
    public void LoadPluFiles() 

    string[] files = GetPlusFiles(); 
    Assembly assembly = Assembly.GetCallingAssembly(); 
    foreach (string file in files) 

    ModuleAttribute[] attributes = Assembly.LoadFile(file).GetCustomAttributes(typeof(ModuleAttribute), false) as ModuleAttribute[]; 
    foreach (ModuleAttribute attribute in attributes) 

    string m = attribute.ModuleType.FullName; 
    string m1 = attribute.ModuleType.Assembly.GetName().Name; 
    object obj = Activator.CreateInstance(attribute.ModuleType); 
    if (obj is IApp) 
    {//无法识别两个模块的接口。 



  • 相关阅读:
    matlab学习笔记10_7数值计算类型和常用计算公式
    matlab学习笔记10_5 通用字符串操作和比较函数
    matlab学习笔记10_6 字符串与数值间的转换以及进制之间的转换
    matlab-数组取值
    matlab学习笔记10_4MATLAB中的字符串表示
    matlab学习笔记10_3关系运算符和逻辑运算符
    matlab学习笔记10_2 一般操作符
    matlab学习笔记10 一般运算符
    matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色
    matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象
  • 原文地址:https://www.cnblogs.com/smilesmile/p/3790948.html
Copyright © 2020-2023  润新知