国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#關(guān)于反射加載的問(wèn)題

2020-01-24 03:43:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
三個(gè)程序集:
主程序集:BaseApp.exe
接口程序集:IBaseApplication
插件程序集:TestAttri
=======================================================================================
在接口程序中:
接口:IApp
屬性定義:ModuleAttribute
復(fù)制代碼 代碼如下:

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類中標(biāo)識(shí)如下
復(fù)制代碼 代碼如下:

[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 = "")]

有兩個(gè)模塊分別是以下
復(fù)制代碼 代碼如下:

namespace TestAttri
{
public partial class UserControl1 : UserControl, IApp
{
……
}
}
namespace TestAttri
{
public partial class UserControl2 : UserControl, IApp
{
……
}
}

=================================================================================================
在主程序集中:
將插件放至到:Application.StartupPath + "http://Plus"
引用了接口程序集“IBaseApplication”
復(fù)制代碼 代碼如下:

/// <summary>
/// 獲取插件文件名稱
/// </summary>
/// <returns></returns>
public string[] GetPlusFiles()
{
return System.IO.Directory.GetFiles(Application.StartupPath + "http://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)
{//無(wú)法識(shí)別兩個(gè)模塊的接口。
}
}
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿巴嘎旗| 东港市| 青冈县| 淳安县| 合山市| 疏附县| 大埔区| 崇仁县| 简阳市| 陆河县| 古田县| 怀安县| 霞浦县| 白城市| 桂东县| 平潭县| 漳平市| 大名县| 含山县| 安远县| 本溪| 岚皋县| 大冶市| 万宁市| 丽水市| 河西区| 大足县| 板桥市| 德州市| 阿合奇县| 宜春市| 桑植县| 监利县| 澄江县| 财经| 东方市| 金湖县| 宁城县| 张家川| 象山县| 平武县|