手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#关于反射加载的问题
C#关于反射加载的问题
摘要:三个程序集:主程序集:BaseApp.exe接口程序集:IBaseApplication插件程序集:TestAttri===========...

三个程序集:

主程序集: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)

{//无法识别两个模块的接口。

}

}

}

}

【C#关于反射加载的问题】相关文章:

C#启动windows服务方法的相关问题分析

C#基础概念二十五问 16-20

浅谈C#基础之类的访问修饰符

C#实现对AES加密和解密的方法

c#中返回文章发表的时间差的示例

基于C#方法重载的总结详解

关于Flyweight模式应用实践的相关介绍

C# char类型字符转换大小写的实现代码

C#中计算时间差中的小数问题解决

C#关机小程序源码

精品推荐
分类导航