开发者

Reflection: How to get the underlying type of a by-ref type

开发者 https://www.devze.com 2023-01-03 03:12 出处:网络
I was surprised to learn that \"ref\" and \"out\" parameters are not marked by a special attribute, despite the existence of ParameterInfo.IsOut, ParameterInfo.IsIn (both of which are always false as

I was surprised to learn that "ref" and "out" parameters are not marked by a special attribute, despite the existence of ParameterInfo.IsOut, ParameterInfo.IsIn (both of which are always false as far as I can see), ParameterAttributes.In and ParameterAttributes.Out. Instead, "ref" parameters are actually represented by a special kind of "Type" object and "out" parameters are just ref parameters with an additional attribute (what kind of attribute I don't yet know).

Anyway, to make a by-ref argument you call Type.MakeByRefType(), but my q开发者_StackOverflow社区uestion is, if you already have a by-ref type, how do you get back to the original Type?

Hint: it's not UnderlyingSystemType:

Type t = typeof(int);
Console.WriteLine(t.MakeByRefType().UnderlyingSystemType==t); // FALSE


Call GetElementType().

0

精彩评论

暂无评论...
验证码 换一张
取 消