c# - (如何)C# 知道对象是什么类型?

标签 c# types gettype object-type

当对象调用.GetType()方法时,C#如何知道它的类型?如果该对象与同一项目中的某些其他对象类型相同怎么办?它们是否具有某种独特的标识(例如 GUID)?

最佳答案

When .GetType() method is called upon an object, how does C# know its type?

除了内存位置之外,CLR 实际上还存储每个对象的类型信息。

这存储在TypeHandle中。详情见Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects ,特别是 Type Fundamentals部分。

当您对对象调用 Object.GetType() 时,CLR 会根据对象引用中的 TypeHandle 进行查找,并返回适当的类型。

关于c# - (如何)C# 知道对象是什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16969382/

相关文章:

c# - C# 中的存储库模式

c# - 在unity3D中使用C#发送邮件?

C# 构造函数有 2 个参数,但声称它没有带两个参数的构造函数

haskell - 与 GHCi 中 fromIntegral 的行为不一致

c# - 类型检查 : typeof, GetType,还是?

c# - 访问按钮父级

C# 确定泛型类型

python - 为什么我会收到 TypeError : can't multiply sequence by non-int of type 'float'

c# - GetType(string) 在 AppDomain.CurrentDomain.Load 之后返回 null

c++ - 如何识别变量的类型