user1861857
user1861857

Reputation: 318

How to extract the return type of a controller method using reflection

In .Net 6

I have a controller with the following method signature

public async Task<ActionResult<MyClass>> Method() {...}

Using reflection, how do I extract the type (MyClass in this example)?

I tried using the Type.GetGenericType method but it doesn't work

Thank you

Upvotes: 0

Views: 558

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9993

You can use ReturnType and GetGenericArguments to extract the type.

//......
var methodinfo =  t.GetMethod("Method");
var result = methodinfo.ReturnType;
var resultType =  result.GetGenericArguments()[0].GetGenericArguments();

Now the resultType is what you want to get.

Upvotes: 1

Related Questions