C#關(guān)于反射加載的問題
更新時(shí)間:2011年07月11日 22:47:14 作者:
C#關(guān)于反射加載的問題,需要的朋友可以參考下。
三個(gè)程序集:
主程序集: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類中標(biāo)識(shí)如下
[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è)模塊分別是以下
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)
{//無法識(shí)別兩個(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 + "\\Plus"
引用了接口程序集“IBaseApplication”
復(fù)制代碼 代碼如下:
/// <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)
{//無法識(shí)別兩個(gè)模塊的接口。
}
}
}
}
您可能感興趣的文章:
- c#反射表達(dá)式樹模糊搜索示例
- c#使用反射調(diào)用類型成員示例
- c#反射機(jī)制學(xué)習(xí)和利用反射獲取類型信息
- C#中使用反射獲取結(jié)構(gòu)體實(shí)例及思路
- 關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(三)
- 關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(二)
- 關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(一)
- C# Dynamic關(guān)鍵字之:dynamic為什么比反射快的詳解
- C#反射(Reflection)對(duì)類的屬性get或set值實(shí)現(xiàn)思路
- C#反射在實(shí)際應(yīng)用中的實(shí)例代碼
- C#反射的一些應(yīng)用
- C#反射實(shí)例學(xué)習(xí)及注意內(nèi)容
- c#中利用委托反射將DataTable轉(zhuǎn)換為實(shí)體集的代碼
- C#反射技術(shù)的簡單操作(讀取和設(shè)置類的屬性)
- c#測(cè)試反射性能示例
相關(guān)文章
Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
.NET利用C#字節(jié)流動(dòng)態(tài)操作Excel文件
在.NET開發(fā)中,通過字節(jié)流動(dòng)態(tài)操作Excel文件提供了一種高效且靈活的方式處理數(shù)據(jù),本文將演示如何在.NET平臺(tái)使用C#通過字節(jié)流創(chuàng)建,讀取,編輯及保存Excel文件,需要的可以參考下2024-12-12
在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼
這篇文章主要介紹了在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼,非常的實(shí)用,這里推薦給大家,希望大家能夠喜歡。2015-03-03
C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法介紹
這篇文章介紹了C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

