【发布时间】:2021-02-22 02:01:30
【问题描述】:
假设一个方法被传递了一个类型。如何在演员表或关键字as的右侧使用。
文档...System.Type
internal static void Test2()
{
Computer c0 = new Computer("Model Z", 123);
Computer c1 = new Computer("Model Z", 456);
Debug.Assert(c0.Equals(c1));
object o0 = c0;
object o1 = c1;
Debug.Assert(o0.Equals(o1) == false);
Debug.Assert((o0 as Computer).Equals(o1 as Computer));
Debug.Assert(((Computer)o0).Equals((Computer)o1));
Type t = typeof(Computer);
//Debug.Assert((o0 as t).Equals(o1 as t));
//Debug.Assert(((t)o0).Equals((t)o1));
Console.WriteLine("END Test2.");
}
internal class Computer
{
private string Description { get; set; }
private int SerialNumber { get; set; }
internal Computer(string d, int sn) { this.Description = d; this.SerialNumber = sn; }
internal bool Equals(Computer other)
{
return this.Description.Equals(other.Description);
}
}
【问题讨论】:
-
您可以创建将对象转换为指定类型的表达式树,将表达式树编译为委托,然后能够将对象传递给委托。但这可能不是你想要的。
标签: .net casting system.type