Assmbly.GetTpes() gets the types in the assembly but if I also wants nested class (OrderLine) how do I do that? I only know the name of the assembly, not the class names so GetType(Order+OrderLine) will not work.
public class Order
{
public class OrderLine
{
}
}
I don't know if assembly.GetTypes()
is supposed to include nested classes. Assuming it doesn't, a method like the following could iterate over all the assembly's types.
IEnumerable<Type> AllTypes(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
yield return type;
foreach (Type nestedType in type.GetNestedTypes())
{
yield return nestedType;
}
}
}
Edit:
MSDN has the following to say about Assembly.GetTypes
The returned array includes nested types.
So really my above answer shouldn't be necessary. You should find both Order
and Order+OrderLine
returned as types by Assembly.GetTypes
.
Something like this:
Assembly.GetTypes().SelectMany(t => new [] { t }.Concat(t.GetNestedTypes()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With