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

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

问题描述

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

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

我有接口 ImyInterface (或某些baseClass)和Attribute myAttribute
我需要一个工厂类,该类为我提供了使用空构造函数搜索所有类型,继承 ImyInterface 并标记有属性 myAttribute 的功能。

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 constructor, inheriting ImyInterface and marked with attribute myAttribute.

我想以不同的模式进行搜索:
1)当前程序集中的搜索类型
2)所有解决方案程序集中的搜索类型
3 )在目标库(.lib或.dll)中的搜索类型

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:

一些寻求的类型实现:

[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");

我已经写了自己的解决方案,但是我担心重新发明轮子 。

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

对这个问题有任何想法/经验吗?

got any ideas/experience 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天全站免登陆