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

﹥>﹥吖頭↗ 提交于 2019-11-30 23:55:55

问题


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 question 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

回答1:


Call GetElementType().



来源:https://stackoverflow.com/questions/3033639/reflection-how-to-get-the-underlying-type-of-a-by-ref-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!