您的位置:首页 > 其它

使用Reflection来查找实现指定接口的类及将其实例化的方法

2011-09-15 09:03 826 查看
使用仿射添加实现了IEncryptAlgorithm接口的类,并实例化,添加到列表中。

Assembly assembly = Assembly.GetExecutingAssembly();    //获取当前执行程序集
Type[] types = assembly.GetTypes();                     //获取程序集中的类
foreach (Type type in types)
{
Type typeInterface = type.GetInterface("IEncryptAlgorithm");    //如果未实现接口,返回值为null
if (typeInterface != null && type.IsInterface == false)
{
//创建实现该接口的对象
IEncryptAlgorithm encryptAlgo = assembly.CreateInstance(type.FullName) as IEncryptAlgorithm;
mArrayAlgorithm.Add(encryptAlgo);
}
}


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