The Type.MakeByRefType method in .NET returns a by-ref version of a type, e.g. passing the type of System.Int32 returns a type representing System.Int32&.
However, if you already have a System.Int32&, what is the mechanism for obtaining a plain old System.Int32? There doesn't seem to be an opposite method to remove the by-ref modifier.
At the moment I've put a hack in place which re-builds the assembly-qualified name without an & at the end of the type name and then loads that type, but this is horribly dirty...
According to the docs for IsByRef, you can use Type.GetElementType.
This is also true for types for which IsArray or IsPointer is true.
var typeInt = typeof(int);
var typeIntRef = typeInt.MakeByRefType();
var typeIntArray = typeInt.MakeArrayType();
var typeIntPointer = typeInt.MakePointerType();
Debug.Assert(typeIntRef.GetElementType() == typeInt);
Debug.Assert(typeIntArray.GetElementType() == typeInt);
Debug.Assert(typeIntPointer.GetElementType() == typeInt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With