首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于类注释或继承类型的扫描程序集和自动绑定

基于类注释或继承类型的扫描程序集和自动绑定
EN

Stack Overflow用户
提问于 2013-02-17 20:32:19
回答 1查看 345关注 0票数 1

我需要扫描所有程序集以查找具有特定属性的类(或从抽象类ColorTest继承的类),并自动将它们绑定到ColorTest。然后,我需要实例化并枚举ColorTest的所有实现。

大会1:

代码语言:javascript
运行
复制
public abstract class ColorTest { 
    public abstract string GetColorString();
}

大会2:

代码语言:javascript
运行
复制
//[ColorImplementation] // attributes are not necessary
public class BlueColor() : ColorTest {...}

大会3:

代码语言:javascript
运行
复制
//[ColorImplementation] //not necessary
public class RedColor() : ColorTest {...}

大会4:

代码语言:javascript
运行
复制
class Program{
    public static Main(){

        #region Problem area :)
        var kernel = new StandardKernel();

        kernel.Bind(x => x
            .FromAssembliesMatching("*")
            .SelectAllClasses()
            .InheritedFrom<ColorTest>// or .WithAttribute<ObsoleteAttribute>()
            .BindAllInterfaces() // I'd like to Bind it only to ColorTest
            .Configure(b => b.InSingletonScope()));
        #endregion

        // Enumerate
        kernel
            .GetAll<ColorTest>()
            .ToList()
            .ForEach(t=>Console.WriteLine(t.GetColorString()));
}

示例是对更复杂问题的抽象和简化。

尼尼姆能帮我处理所描述的场景吗?如果是,怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-17 22:28:56

解决办法很简单!

我不得不换了两行:

代码语言:javascript
运行
复制
.FromAssembliesMatching("*") -> .FromAssembliesMatching(".") //mistake

代码语言:javascript
运行
复制
.BindAllInterfaces() -> .BindBase() // because I'm implementing 
                                    // abstract class, not interface

不涉及属性

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14925740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档