如何在 C# 代码中知道声明变量的类型
我想要一些函数,如果将 Base
类的变量传递给它,则返回“Base”,如果将其声明为 Derived
,则返回“Derived”,等等。取决于分配给它的值的运行时类型。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想要一些函数,如果将 Base
类的变量传递给它,则返回“Base”,如果将其声明为 Derived
,则返回“Derived”,等等。取决于分配给它的值的运行时类型。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
例如,请参阅下面的代码。关键是使用泛型,使用扩展方法只是为了好的语法。
结果:
编辑:
您还可以避免在此处使用扩展方法,因为这会导致它出现在每个 IntelliSense 下拉列表中。看另一个例子:
也产生正确的结果。
See code below for example. The key is to use Generics, extension method was used just for nice syntax.
Result:
EDIT:
You may also avoid using extension method here, as it would cause it to appear on every IntelliSense drop-down list. See another example:
also produces correct results.
如果不解析相关代码,这是不可能的。
在运行时,只有两部分类型信息可用,即值的实际类型(通过 object.GetType()),如果所讨论的变量是参数或类/实例变量,则 FieldInfo 上的 FieldType 属性,ParameterType 位于 ParameterInfo 上。
由于传递给您的值很可能是通过传递给您的路径上的多个变量来的,所以恐怕这个问题甚至没有明确定义。
啊 - 我看到你只想要方法中当前定义的类型,表达式功能将提供这个(Roman的答案显示了一个简洁的方法来做到这一点),但要小心尝试在方法之外使用它......本质上你让编译器的泛型类型推断会推断出相关类型,但这意味着所使用的变量并不总是您可以看到的变量。它可能是编译器合成变量的变量,例如:
由于编译器合成了一个临时变量,在其中放置对 x 的对象引用。
This is not possible without parsing the code in question.
At runtime only two pieces of type information are available, the actual type of a value (via object.GetType()) and, if the variable in question is a parameter or class/instance variable the FieldType property on a FieldInfo, PropertyType on a PropertyInfo or ParameterType on a ParameterInfo.
Since the value passed to you may well have come via several variables on its route to you the question is not even well defined I'm afraid.
Ah - I see you want only the type as currently defined in the method, the Expression functionality will provide this (Roman's answer shows a neat way to do this) but beware trying to use this outside the method... In essence you are letting the generic type inference of the compiler infer the type in question but this means that the variable used is not always the variable you can see. It may instead be that of a compiler synthesised variable, for example:
Since the compiler synthesises a temporary variable in which to place an object reference to x.
只需递归 GetType() 直到命中对象。
Just recurse on GetType() until you hit object.