简体   繁体   English

如何获取对象的实际类型

[英]How to get the actual type of object

我想获得对象的实际类型,即使我将其转换为其他类型,例如我有一个Integer并将其转换为Object ,有没有办法找出方法呢?

.NET has .GetType() as part of every object. .NET将.GetType()作为每个对象的一部分。 You could use this to do stuff with reflection. 您可以使用它来进行反射操作。

C# also has pattern matching: C#还具有模式匹配:

if (obj is int intVal)
{
    // use as an integer
}

When you cast or box any value from one type to System.Object , it still under the covers has the same value. 当您将任何值从一种类型投射或装箱到System.Object ,它仍然在幕后具有相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM