C#關於反射載入的問題

來源:互聯網
上載者:User

三個程式集:
主程式集: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)
{//無法識別兩個模組的介面。
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.