反思:如何获取by-ref类型的基础类型 [英] Reflection: How to get the underlying type of a by-ref type

查看:106
本文介绍了反思:如何获取by-ref类型的基础类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现,尽管存在ParameterInfo.IsOut,ParameterInfo.IsIn(据我所见,这两个都是错误的),但 ref和 out参数并未被特殊属性标记,ParameterAttributes.In和ParameterAttributes.Out。取而代之的是, ref参数实际上由一种特殊的 Type对象表示,而 out参数只是具有附加属性(我尚不知道哪种属性)的ref参数。

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).

无论如何,要创建一个by-ref参数,您可以调用Type.MakeByRefType(),但是我的问题是,如果您已经有了一个by-ref类型,如何返回原始类型?

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?

提示:它不是UnderlyingSystemType:

Hint: it's not UnderlyingSystemType:

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


推荐答案

调用 GetElementType()

这篇关于反思:如何获取by-ref类型的基础类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆