0

I want to be able do something like this

void SampleFunction(Type type)
{
    type varA = default(type);
    OtherGenericFunction<type>();
}

Tried using typeof(type) but it didn't gave any result.

I tried searching it, but did not understand solutions or how to apply them, these I looked:

How to use local variable as a type?

Passing just a type as a parameter in C#

Can someone explain how to deal with situation in my case?

Just to give some context: I trying to mock some generic functions. As straightforward mock of generic doesn't working - my current theoretical solution is to gather List<Type> of used types in function Im testing, then just iterate through it and setup my mock object.

42ama
  • 141
  • 9
  • 4
    `Void SampleFunction() { OtherGenericFunction(); }`? – 41686d6564 stands w. Palestine Sep 02 '19 at 16:32
  • 3
    If you *absolutely must* do this, you need to use reflection; in particular `openGenericMethod.MakeGenericMethod(type).Invoke(...)` where `openGenericMethod` is what you get from `type.GetMethod("OtherGenericFunction")`. It is inefficient, though; fine for mocking/testing, I guess – Marc Gravell Sep 02 '19 at 16:37
  • Thanks, both comments helped me to form a little more stable picture of whats happening – 42ama Sep 02 '19 at 16:47
  • Why don't you include your actual code and which mocking framework you use? –  Sep 02 '19 at 17:24

0 Answers0