Thank you 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. Possibly it's because the initializing xaml conflicts with it.
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.). 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);