C#中的Type.Equals()方法确定当前Type的基础系统类型是否与指定的Object或Type的基础系统类型相同。
public virtual bool Equals (Type o); public override bool Equals (object o);
上面的参数是将其基础系统类型与当前Type的基础系统类型进行比较的对象。
现在让我们看一个实现Type.Equals()方法的示例-
using System; public class Demo { public static void Main(string[] args) { Type val1 = typeof(System.UInt16); Type val2 = typeof(System.Int32); Console.WriteLine("Are both the types equal? "+val1.Equals(val2)); } }
输出结果
这将产生以下输出-
Are both the types equal? False
现在让我们来看另一个实现Type.Equals()方法的示例-
using System; using System.Reflection; public class Demo { public static void Main(string[] args) { Type type = typeof(String); Object obj = typeof(String).GetTypeInfo(); Type type2 = obj as Type; if (type2 != null) Console.WriteLine("Both types are equal? " +type.Equals(type2)); else Console.WriteLine("无法投射!"); } }
输出结果
这将产生以下输出-
Both types are equal? True