0

I would like to create a function that gets a string parameter, determines 2 types based on that parameter and passes that two types to a generic.

public void Foo(string a) 
{  
var typeb = typebProvider.GetType(a);   
var typec = typecPrivoder.GetType(a);  
var d = factory.Create<typeb,typec >();
}

the problem is i am getting 'typeb' is a variable but is used like a type. What should I change to get is to work?

Huak
  • 1
  • You'll want something like `factory.GetType().GetMethod("Create").MakeGenericMethod(typeb, typec).Invoke(factory, Array.Empty())` – Mathias R. Jessen Nov 24 '22 at 16:52
  • Now, the more important question is: Assuming you got that to work somehow, then how are you going to use the instance in `d` in your source code? You can't use any of the members declared by the respective generic type of the `d` instance in your source code, as the generic type parameters and therefore that concrete generic type is not yet known at compile/build time. Have you thought about that? Also, what exactly is the type of the `factory` instance? Perhaps it has methods to achieve this in a non-generic way. –  Nov 24 '22 at 16:52
  • Otherwise, does this answer your question? [Select Right Generic Method with Reflection](https://stackoverflow.com/questions/3631547/select-right-generic-method-with-reflection) –  Nov 24 '22 at 16:53
  • Factory Pattern and a matching interface for typea and typeb are the way – ChrisBD Nov 24 '22 at 17:04

0 Answers0