简体   繁体   English

.NET Windows应用商店应用和System.Type - 相当于运营商==?

[英].NET Windows Store Apps and System.Type - equivalent to operator ==?

According to the accepted answer to this StackOverflow question, there is a difference between the System.Type.Equals method and the System.Type operator == : 根据公认的答案, 这个 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.

In .NET for Windows Store Apps , the System.Type operator == is not available. .NET for Windows Store应用程序中System.Type运算符==不可用。

How can I completely reproduce the functionality of the System.Type operator == in a Windows Store application ? 如何在Windows应用商店应用程序中完全重现System.Type operator ==的功能? Or, is the specific equality operator functionality for System.Type not relevant in Windows Store apps ? 或者, System.Type的特定相等运算符功能是否与Windows应用商店应用无关?

So the underlying principle here is that Equals is a virtual instance method of every type, meaning it will use the vtable to determine the implementation to use, at runtime, based on the type of the actual object that called Equals (disregarding the type of the variable). 所以这里的基本原则是Equals是每种类型的虚拟实例方法,这意味着它将使用vtable在运行时根据调用Equals的实际对象的类型来确定要使用的实现(忽略类型变量)。

operator == can be thought of as a static method (with a lot of overloads). operator ==可以被认为是一个静态方法(有很多重载)。 It is not virtual, so the implementation that is referred to will be based on the compile time type of the variable holding the object, and won't be based on what the actual type of the object is at runtime. 它不是虚拟的,因此所引用的实现将基于保存对象的变量的编译时类型,并且不会基于对象的实际类型在运行时的内容。

This behavior can be replicated by simply creating your own static Equals methods (on whatever type or types you prefer) that behave based on the types of the two arguments. 可以通过简单地创建自己的static Equals方法(根据您喜欢的任何类型或类型)来复制此行为,该方法基于两个参数的类型而行为。 This is slightly less convenient to type out than using operator == , but does effectively the same thing once compiled. 输出比使用operator ==稍微不方便,但是一旦编译就有效地做同样的事情。

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

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