1

In the below example I have a method called GetList that takes a single string parameter and returns a List entities. I capture this in a generic IEnumerable variable because at runtime I have no idea what entity the user may want. How can I use the actual type instead of object?

I want to replace this...

IEnumerable<object> data = GetList(entityName);

With this...

IEnumerable<Company> data = GetList(entityName);

The only way I can think of handling it right now which I'm NOT going to do because we have 300+ entities is something like

switch(entitName)
{
  case "Company":
   IEnumerable<Company> data = GetList(entityName);
   break;

  case "Employee":
   IEnumerable<Employee> data = GetList(entityName);
   break;

   ...
}
jjf1978
  • 199
  • 1
  • 2
  • 13
  • `GetList(entityName).Cast()` – Lucas Trzesniewski Sep 23 '16 at 16:22
  • I don't know it will be Company though. It could be Employee or Role etc..The only information I have is a string (entityName) representing the entity they want to query. – jjf1978 Sep 23 '16 at 16:22
  • Well, in this case you should keep your object. The compiler can't know for you neither. You will have to make some type cheking to know what type you are using then. Or you will need to give us the context where your code is executed to allow us to find a work around. – romain-aga Sep 23 '16 at 16:24
  • @romain-aga I was afraid that was the answer :( – jjf1978 Sep 23 '16 at 16:25
  • 1
    What are you planning on doing with the values if you don't know what type they are? – Lee Sep 23 '16 at 16:41
  • I need to convert the IEnumerable to a DataTable. But without knowing the type for object I cannot do this. – jjf1978 Sep 23 '16 at 16:42
  • 1
    How can you 'do anything' with the data if you don't know what type you expect? Do you just need to process all properties generically or are there certain properties you expect to exist? – Lee Sep 23 '16 at 16:45
  • @Lee I don't know ask my boss this is what he wants I'm like 10 minutes away from quitting this bullshit job lol – jjf1978 Sep 23 '16 at 16:46
  • 1
    @jjf1978 Basically what you're asking for is to make information available to the compiler that isn't available until runtime. Another term for being able to do that is "predicting the future". – Kyle Sep 23 '16 at 16:50

2 Answers2

3

You can't use such type in complied code - so there is really no way to create it without reflection.

Closest you can get to what you seem to want is IEnumerable<dynamic> so you can access property of object based on they run-time type.

If you just need to create strongly typed enumerable to return to some other code expecting better than IEnumerable<object> you can use reflection to create such type (Create generic List<T> with reflection).

You may also be able to have generic function that accesses result in strongly typed manner but instead of calling it directly call it via reflection after using MakeGenericMethod with correct type (How do I use reflection to call a generic method?).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

You can make GetList() generic:

IEnumerable<Company> data = GetList<Company>(entityName);

IEnumerable<T> GetList<T>(string entityName)
{
    List<T> list = new List<T>();

    // Populate list here

    return list;
}
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • I cant specify the type because all I have to go on is a string. The end user clicks a list which returns a string to my application then the application has to go fetch that entity. So I cant hardcode GetList – jjf1978 Sep 23 '16 at 16:28
  • 2
    How do you expect to do anything meaningful with the list you get back if you don't know anything about it? – itsme86 Sep 23 '16 at 16:29
  • That is exactly the problem. About all I can do with the returned list is databind it. I can use reflection to painfully look at all the properties but I really don't want to do that... Basically I want a variable type – jjf1978 Sep 23 '16 at 16:30