您的位置:首页 > 其它

通过反射来判断某个程序集中是否有实现该接口的类

2009-06-23 22:41 417 查看
1: private IPlugin LoadPlugin( string AssemblyFileName )

2: {

3:     IPlugin PluginFound = null;

4:     Type iPluginType = typeof(IPlugin);

5:

6:     Assembly _Assembly = Assembly.LoadFrom(AssemblyFileName);

7:

8:     if (_Assembly != null)

9:     {

10:         Type[] types = _Assembly.GetExportedTypes();

11:

12:         foreach (Type t in types)

13:             if (iPluginType.IsAssignableFrom(t))

14:             {

15:                 IPlugin operation = Activator.CreateInstance(t) as IPlugin;

16:

17:                 if (operation != null)

18:                 {

19:                     PluginFound = operation;

20:                     break;

21:                 }

22:             }

23:     }

24:

25:     return PluginFound;

26: }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐