简体   繁体   English

在Metro中,获取(抽象)类的所有继承的类?

[英]In metro, get all inherited classes of an (abstract) class?

I got stuck when trying to get all inherited classes in metro. 尝试在Metro中获取所有继承的类时,我陷入了困境。 The WinRT API is different from .net framework, so both the solutions ( solution1 and solution2 ) don't work. WinRT API与.net框架不同,因此两个解决方案( solution1solution2 )均不起作用。

Any code or tool solution is appreciated. 任何代码或工具解决方案都值得赞赏。

I'm still trying. 我还在努力 I will put my solution here if success. 如果成功,我将把解决方案放在这里。

This should work (based on the code from here ): 这应该可以工作(基于此处的代码):

public IEnumerable<T> GetEnumerableOfType<T>(params object[] constructorArgs) where T : class, IComparable<T>
{
    List<T> objects = new List<T>();
    foreach (Type type in typeof(T).GetTypeInfo().Assembly.ExportedTypes
                                   .Where(myType => myType.GetTypeInfo().IsClass && 
                                                    !myType.GetTypeInfo().IsAbstract && 
                                                    myType.GetTypeInfo().IsSubclassOf(typeof(T))))
    {
        objects.Add((T)Activator.CreateInstance(type, constructorArgs));
    }
    objects.Sort();
    return objects;
}

Basically TypeInfo is now the main class for doing any reflection operations and you can get it by calling GetTypeInfo() on the Type . 现在基本上, TypeInfo是执行任何反射操作的主要类,您可以通过在Type上调用GetTypeInfo()来获取它。

Thank you Damir Arh. 谢谢达米尔·阿尔(Damir Arh)。 Your solution is great although it only needs little modification. 您的解决方案很棒,尽管只需要很少的修改即可。

One thing I observed is your static method can't be put into MainPage class, or compile-error would happen. 我观察到的一件事是您的静态方法无法放入MainPage类,否则会发生编译错误。 Possibly it's because the initializing xaml conflicts with it. 可能是因为初始化xaml与之冲突。

On the other hand, I altered the original code to simpler and useful one with much less run-time error (The line in objects.Add((T)Activator.CreateInstance(type, constructorArgs)); often leads to run-time error in many cases.). 另一方面,我将原始代码更改为更简单和有用的代码,并且运行时错误少得多( objects.Add((T)Activator.CreateInstance(type, constructorArgs));通常会导致运行时错误在许多情况下。)。 The main purpose is just to get all inherited classes after all. 毕竟,主要目的只是获得所有继承的类。

    // GetTypeInfo() returns TypeInfo which comes from the namespace
    using System.Reflection;

    public List<Type> GetEnumerableOfType<T>(params object[] constructorArgs) where T : class
    {
        List<Type> objects = new List<Type>();

        IEnumerable<Type> s = typeof(T).GetTypeInfo().Assembly.ExportedTypes.Where(myType => myType.GetTypeInfo().IsClass &&
                                                         !myType.GetTypeInfo().IsAbstract &&
                                                         myType.GetTypeInfo().IsSubclassOf(typeof(T)));

        foreach (Type type in s)
            objects.Add(type);

        return objects;
    }

And the following is a test sample. 以下是测试样本。

     var list = GetEnumerableOfType<UIElement>();

     foreach (var v in list)
         Debug.WriteLine(v.Name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM