【发布时间】:2013-06-17 13:21:27
【问题描述】:
我有解决下一个问题的模式或内部解决方案:
我有接口 ImyInterface(或一些基类)和属性 myAttribute。 我需要一个工厂类,它为我提供了使用空构造函数搜索所有类型、继承 ImyInterface 并用属性 myAttribute 标记的功能。
我想以不同的模式搜索: 1) 在当前程序集中搜索类型 2) 在所有解决方案程序集中搜索类型 3) 目标库中的搜索类型(.lib 或 .dll)
结果我想看到这样的东西:
一些受欢迎的类型实现:
[myAttribute(uniqueTypeNameOrSomethingLikeThat = "SuperClass")]
public class mySomeResource:ImyInterface
{
mySomeResource(){...}
}
工厂使用:
myResourceFactory factory = new myResourceFactory(typeof(ImyInterface), typeof(myAttribute));
factory.ScanThisAssembly();
factory.ScanDirrectory("C:\\libraries");
var atlast = (ImyInterface)factory.Create("SuperClass");
我已经编写了自己的解决方案,但我害怕“重新发明轮子”。
对这个问题有任何想法/经验吗?
【问题讨论】:
标签: c# reflection design-patterns factory