0
public ActionResult Customer(string fullNameSpace)
{
    var genType = fullNameSpace.ToGenericType(); //how can I do this?
    var clList = _adminService.GetCustomerList<genType>(23);
    return PartialView(clList);
}


public IEnumerable<CustomerVM> GetCustomerList<S>(int clientId) where S : IEntityVMSortOrder{
    ...
}

Without changing the GetCustomerList function, how can I produce genType from the passed string argument? If this is possible at all.

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • 1
    What does `sortProperty` contain that you think is convertable to whatever `ToGenericType` returns? What does `ToGenericType` do? – NetMage Feb 20 '19 at 23:58
  • its a full namespace – pnizzle Feb 20 '19 at 23:59
  • @NetMage I have changed it to fullNameSpace so it can make sense. Thanks for that one – pnizzle Feb 20 '19 at 23:59
  • I don't think saying `fullNameSpace` contains a full namespace makes any sense. – NetMage Feb 21 '19 at 00:00
  • It does lol. I am passing a class's full namespace plus class name into that string. – pnizzle Feb 21 '19 at 00:00
  • So what you mean is `fullNameSpace` contains a fully qualified class name, and you want to lookup that type? What does generic have to do with it? Perhaps you are looking for `Type.GetType`? – NetMage Feb 21 '19 at 00:03
  • @NetMage you are completely missing my question simply because you are trying to make sense of the logic in the code. I simply need to convert a fully qualified namespace as you called it to a type that i can then pass onto the specified function there. – pnizzle Feb 21 '19 at 00:05
  • 1
    Types in `<>` are compile time entities. It isn't possible to put a variable there, but you can use Reflection as shown [here](https://stackoverflow.com/a/1606988/2557128) – NetMage Feb 21 '19 at 00:05
  • @NetMage you are right in that the generic part of my question doesn't make sense. I have removed it. `ToGenericType ` is not an actual function, hence the comment next to it. – pnizzle Feb 21 '19 at 00:11
  • You need to use `System.Type.GetType(fullNameSpace)` and then the duplicate applies. – Enigmativity Feb 21 '19 at 00:18
  • @pnizzle - Please look at the duplicate and the hint I gave with `GetType` and then compare with this: `var genType = System.Type.GetType(fullNameSpace); MethodInfo method = typeof(AdminService).GetMethod("GetCustomerList"); MethodInfo generic = method.MakeGenericMethod(genType); var clList = (IEnumerable)generic.Invoke(_adminService, new object[] { 42 });`. That should be everything you need. – Enigmativity Feb 21 '19 at 00:39
  • @Enigmativity Ahh, right I see. Thanks for that. Am all new to this. So the approach would have been a duplicate. But the question itself is not a duplicate right? The answer to my question is to use reflection to call the generic function, of which how to do that is already answered somewhere else. But that doesn't make my question a duplicate though. Just my opinion... Cheers – pnizzle Feb 21 '19 at 00:44

0 Answers0