0

Assuming i have a base entity class:

public abstract partial class Person
    {
        public Person()
        {
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }


    }

and i have multiple entities that inherit from that entity e.g:

public partial class Customer : Person
{
    public Customer()
    {
    }

    public int CustomerStatusId { get; set; }
    public string AccountNumber { get; set; }

}

and a few more entities that inherit from the Person class.

is there a way i can get all the Entities class names that inherit from the Person class? i would like to display those in a select box, to create a generic AddUser method.

Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
  • Take a look at this post, I think this is what you need: http://stackoverflow.com/questions/2480691/getting-all-types-from-an-assembly-derived-from-a-base-class – Rob Nov 12 '13 at 07:42
  • I *assume* you only want to know about implementations within (the current assembly/a limited set of assemblies) because, of course, this being a public class there could be an infinite number of inheriting classes. – Damien_The_Unbeliever Nov 12 '13 at 07:45
  • @Damien_The_Unbeliever yes, you are right. – Rafael Herscovici Nov 12 '13 at 07:48

3 Answers3

2
var names = assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t)).Select(t => t.Name);

Try something like the above to get just the names.

Use the following answer for reference How to find all the types in an Assembly that Inherit from a Specific Type C#

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
1

You need to use reflections:

using System.IO;
using System;
using System.Reflection;
using System.Linq;

class Program
{
    static void Main()
    {
        Assembly a = typeof(Program).Assembly;

        var types = a.GetTypes().Where(i=>i.IsSubclassOf(typeof(T)));

        foreach(var i in types)
        {
            Console.WriteLine(i);
        }
    }
}

public class T {  }
public class TT : T {  }
public class TTT : T {  }
Michael Mairegger
  • 6,833
  • 28
  • 41
1

Reflection is the easiest way to go (as suggested in other answers).

Yet (since you tagged the question with entity-framework and you mentioned Entities) in case your intent is to get list of derived types that exists in the database (to fill up a lookup) you can try this:

var typeNames = context.Set<Person>()
                       .ToList()
                       .Select(x => x.GetType().Name)
                       .Distinct();
haim770
  • 48,394
  • 7
  • 105
  • 133
  • Interesting. i usually try to avoid using reflection, but i wonder which one would be more "costly", my guess would be that another db call would be more then the reflection one. – Rafael Herscovici Nov 12 '13 at 07:53
  • 1
    if you mean "costly" in terms of performance, Reflection would probably be much faster, especially if your table contains many `Person`. however, if you require your drop-down list to only contain types that *already exists* in the database, i guess you would have to use that solution. – haim770 Nov 12 '13 at 07:58