Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert between a Ref parameter type and the non-ref version in C#

Tags:

c#

I'm looking at the parameters of a method and extracting the types. I get back (for instance) "System.String&", because the parameter is an out parameter. I want to know whether the parameter is a String - but there doesn't seem to be a way of converting a String& to its non-ref counterpart.

Can anyone point me in the right direction?

Thanks!

like image 569
Andrew Ducker Avatar asked Dec 28 '22 09:12

Andrew Ducker


1 Answers

Type.GetElementType

The Type of the object encompassed or referred to by the current array, pointer, or reference type, or null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.

Example:

var stringRefType = typeof(string).MakeByRefType();
var stringType = stringRefType.GetElementType();
Console.WriteLine(stringType == typeof(string)); // True
like image 161
Quartermeister Avatar answered Feb 05 '23 09:02

Quartermeister