c# - .NET Windows Store Apps 和 System.Type - 相当于运算符 ==?

标签 c# .net windows-store-apps equality system.type

根据对 this 的公认答案StackOverflow 问题,System.Type.Equals 方法和System.Type operator == 有区别:

a runtime type (represented by the internal type RuntimeType), managed by 
the CLR is not always the same as a Type, which can be extended. Equals 
will check the underlying system type, whereas == will check the type itself.

.NET for Windows Store Apps 中,System.Type operator ==不可用。

如何在 Windows 应用商店应用程序 中完全重现 System.Type 运算符 == 的功能?或者,System.Type 的特定相等运算符功能是否与 Windows 应用商店应用 无关?

最佳答案

所以这里的基本原理是 Equals 是每个类型的虚拟实例方法,这意味着它将使用 vtable 来确定要使用的实现,在运行时,基于实际的类型调用 Equals 的对象(忽略变量的类型)。

operator == 可以被认为是一个静态方法(有很多重载)。它不是虚拟的,因此引用的实现将基于保存对象的变量的编译时类型,而不是基于对象在运行时的实际类型。

可以通过简单地创建您自己的 static Equals 方法(在您喜欢的任何类型上)来复制此行为,这些方法的行为基于两个参数的类型。与使用 operator == 相比,这在输入时略显不方便,但一旦编译就可以有效地完成同样的事情。

关于c# - .NET Windows Store Apps 和 System.Type - 相当于运算符 ==?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960573/

相关文章:

c# - 有没有办法调用需要 IEnumerable<T> 与单个值的 C# 方法? ...与基准测试

c# - 将异常写入 Windows 日志文件

c# - 如何从.net应用程序编译java代码?

c# - XamlCompiler 错误 WMC1006 : Cannot resolve Assembly or Windows Metadata file 'XXX.YYY.ZZZ'

c# - 如何处理具有未实现 IDisposable 属性的类?

c# - 使用 Messsage Service Client for .net 连接到 IBM WMQ 时如何传递身份验证信息

.net - 忽略 Perforce 文件夹差异中的文件夹

windows-8 - Windows 应用商店应用程序 : Should I use ListBox or ListView?

c# - WinStore : Slide a Grid on- and off-screen

c# - 将 UTC 日期时间全局转换为用户指定的本地日期时间