C#關(guān)于反射加載的問題
更新時間:2011年07月11日 22:47:14 作者:
C#關(guān)于反射加載的問題,需要的朋友可以參考下。
三個程序集:
主程序集: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)識如下
[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
復(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)識如下
復(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 = "")]
有兩個模塊分別是以下
復(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)
{//無法識別兩個模塊的接口。
}
}
}
}
您可能感興趣的文章:
- c#反射表達(dá)式樹模糊搜索示例
- c#使用反射調(diào)用類型成員示例
- c#反射機(jī)制學(xué)習(xí)和利用反射獲取類型信息
- C#中使用反射獲取結(jié)構(gòu)體實(shí)例及思路
- 關(guān)于C#基礎(chǔ)知識回顧--反射(三)
- 關(guān)于C#基礎(chǔ)知識回顧--反射(二)
- 關(guān)于C#基礎(chǔ)知識回顧--反射(一)
- C# Dynamic關(guān)鍵字之:dynamic為什么比反射快的詳解
- C#反射(Reflection)對類的屬性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#測試反射性能示例
相關(guān)文章
C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例
下面小編就為大家分享一篇C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12C#使用DeflateStream解壓縮數(shù)據(jù)文件的方法
這篇文章主要介紹了C#使用DeflateStream解壓縮數(shù)據(jù)文件的方法,較為詳細(xì)的分析了DeflateStream方法對文件進(jìn)行壓縮及解壓縮的步驟與技巧,需要的朋友可以參考下2015-04-04C# winform點(diǎn)擊生成二維碼實(shí)例代碼
這篇文章主要介紹了 C# winform點(diǎn)擊生成二維碼實(shí)例代碼,需要的朋友可以參考下2017-04-04C#中Backgroundworker與Thread的區(qū)別
本文主要介紹了C#中Backgroundworker與Thread的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06