如何使用反射来解决此问题。。。。?(C#/.NET)

如何使用反射来解决此问题。。。。?(C#/.NET),.net,reflection,c#-3.0,.net,Reflection,C# 3.0,我正在开发C#/.NET 3.5应用程序(并希望继续使用该版本的.NET),但我不知道如何使用反射解决此问题。我找到了解决办法,但并不“整洁”。 代码如下,我需要发现所有接口实现,以便将来添加更多接口实现时,我不需要更改现有代码 interface Ii { } class A : Ii { } class A1 : A { } class A2 : A { } class A3 : A { } class B : Ii { } class C : Ii{ } // maybe in futur

我正在开发C#/.NET 3.5应用程序(并希望继续使用该版本的.NET),但我不知道如何使用反射解决此问题。我找到了解决办法,但并不“整洁”。 代码如下,我需要发现所有接口实现,以便将来添加更多接口实现时,我不需要更改现有代码

interface Ii { }
class A : Ii { }
class A1 : A { }
class A2 : A { }
class A3 : A { }
class B : Ii { }
class C : Ii{ }
// maybe in future class D : Ii { }
// maybe in future class E : Ii { }

class Helper
{
    static List<Type> GetAllInterfaceImplemenations()
    {// do reflection magic and return ["A1","A2","A3","B","C"] ...
     // I will use this method to fill comboBox-es , create objects factory, etc...
     // there should be no changes if/when in future I add class D etc.
    }
}
接口Ii{}
A类:Ii{}
A1类:A{}
A2类:A{}
A3类:A{}
B类:Ii{}
C类:Ii{}
//也许在未来的D班:Ii{}
//也许在未来的E班:Ii{}
类助手
{
静态列表GetAllInterfaceImplements()
{//执行反射魔法并返回[“A1”、“A2”、“A3”、“B”、“C”]。。。
//我将使用此方法填充组合框,创建对象工厂等。。。
//如果将来我添加D类等,则不会有任何更改。
}
}
试试这个:

public static List<string> GetAllInterfaceImplemenations()
{
    var interfaceType = typeof(Ii);
    var list = new List<string>();
    foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
    {
        if (type.IsClass && interfaceType.IsAssignableFrom(type))
        {
            list.Add(type.Name);
        }
    }

    return list;
}
public静态列表getAllInterface实现()
{
var interfaceType=类型(Ii);
var list=新列表();
foreach(Assembly.getExecutionGassembly().GetTypes()中的变量类型)
{
if(type.IsClass&&interfaceType.IsAssignableFrom(type))
{
列表.添加(类型.名称);
}
}
退货清单;
}

上述解决方案的问题是,它将返回上面示例中的类“A”,这是不需要的。只需要“叶子”。但是,上面的解决方案给出了解决方法。所以,这里是我的生产解决方案(对不起,伙计们,ArrayList是我最喜欢的收藏…)


好的,修好了。现在我们使用List。我想要字符串列表(类名),但这没有问题。Select(x=>x.Name)可以做到。我想要字符串列表(类名),但这没有问题。问题是,它也会返回“A”,这在本例中是不需要的。但是,这已经很好了…将类A标记为抽象类怎么样?这将是区分不应该创建的类和应该创建的类的干净方法。这里有人问过类似的问题,你可以看看我的答案:既然你想使用不应该使用ArrayList的类型。更好的使用列表
private static ArrayList GetAllInterfaceImplemenations()
{
    ArrayList listOfLeafsNames = null;
    try
    {
        Type interfaceType = typeof(ISpeechCodec);
        ArrayList listOfAll = new ArrayList();
        foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
        {
            if (type.IsClass && interfaceType.IsAssignableFrom(type))
            {
                listOfAll.Add(type);
            }
        }

        listOfLeafsNames = new ArrayList();
        foreach (Type typeCandidate in listOfAll)
        {
            bool isLeaf = true;
            foreach (Type type2 in listOfAll)
            {
                if (!(typeCandidate.Equals(type2)))
                {
                    if (typeCandidate.IsAssignableFrom(type2))
                    {
                        isLeaf = false;
                    }
                }
            }
            if (isLeaf)
            {
                listOfLeafsNames.Add(typeCandidate.FullName);
            }
        }
    }
    catch (Exception ex)
    {
        Setup_TraceExceptions(ex.ToString());
    }

    return listOfLeafsNames;
}