Skip to main content
 首页 » 编程设计

c#之this.Type 保留应报

2024年02月24日25artech

我昨天发现我不能有一个使用名为“Type”的字段的类,这可能是保留的。

虽然该字段可能被保留,但如果它没有定义为公共(public)字符串的类型字段,您仍然不能设置 anyObject.Type。忽略任何 getter 和 setter 并直接跳到将 YourObject 更改为“whatever”字符串。

亲自尝试一下。定义一个类型字段并尝试设置它。

这应该报告给 Microsoft,这样以后就没有人会使用“类型”作为字段,在尝试定义它时不会出现警告/错误。

        public Point_Extended(Point p, booking b) 
        { 
            this.ID = p.ID; 
            this.Type = p.Type; 
            this.Status = p.Status; 
            this.Size = p.Size; 
            this.Coords = p.Coords; 
            this.Color = p.Color; 
            this.BuildingID = p.BuildingID; 
 
            this.Login = b.Login; 
            this.Starts = b.Starts; 
            this.Hours = b.Hours; 
            this.BookingID = b.ID; 
        } 

请您参考如下方法:

如果名称有歧义 - 只需使用 this.Type/obj.Type(实例),TypeName.Type(静态)或 System.Type(类型)。或者在非常糟糕的情况下,global::System.Type。这很好用并且符合问题(我认为):

static class Program 
{ 
    static void Main() { 
        Test anyObject = new Test(); 
        anyObject.Type = "abc"; 
    } 
} 
class Test 
{ 
    public string Type; 
}