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

来源:互联网 发布:linux开机运行脚本 编辑:程序博客网 时间:2024/06/05 17:55

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: }
原创粉丝点击