C#关于反射加载的问题
程序员文章站
2023-08-17 11:29:39
三个程序集: 主程序集: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)
{//无法识别两个模块的接口。
}
}
}
}
主程序集: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)
{//无法识别两个模块的接口。
}
}
}
}