C#自动扫描工厂 [英] C# Auto-scaning factory

查看:190
本文介绍了C#自动扫描工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任何模式或内部解决方案来解决下一个问题:



我有界面 ImyInterface (或一些baseClass)和属性的 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:\\\);

var atlast =(ImyInterface)factory.Create(SuperClass);

我已经写了我自己的解决方案,但我担心重塑轮子



有任何想法/有关此问题的期限?

解决方案

该模式称为反转控制。大约有一百万个依赖注入容器解决这个问题,每个容器都有自己的正面和负面的怪癖。选择一个你最喜欢的或自己滚动,如果不适合你的需要。


I there any patterns or internal solutions for solving next problem:

i've got interface ImyInterface(or some baseClass) and Attribute myAttribute. I need a factory class, that provides me functionality for searching all types with empty constuctor, inheriting ImyInterface and marked with attribute myAttribute.

I wanna search in different modes: 1) search types in current assembly 2) search types in all solution assemblies 3) search types in target library (.lib or .dll)

at result i wanna see something like this:

Some sought-for type implementation:

[myAttribute(uniqueTypeNameOrSomethingLikeThat = "SuperClass")]
public class mySomeResource:ImyInterface
{
   mySomeResource(){...}
}

Factory using:

  myResourceFactory  factory = new myResourceFactory(typeof(ImyInterface), typeof(myAttribute));

  factory.ScanThisAssembly();
  factory.ScanDirrectory("C:\\libraries");

  var atlast = (ImyInterface)factory.Create("SuperClass");

I've already wrote my own solution, but i'am in fear of "reinventing the wheel".

got any ideas/expirience about this problem?

解决方案

That pattern is called Inversion of control. There are about a million dependency injection containers solving this problem, each with their own positive and negative quirks. Pick one that you like best or roll your own if none fits your needs.

这篇关于C#自动扫描工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆