简体   繁体   English

如何告诉C#在对象的基类中查找属性?

[英]How to tell C# to look in an object's base class for a property?

I'm getting the error " T does not contain a definition for Id " below in the specified line, even though when I debug, I see that "item" does indeed have a property "Id" in its base class . 即使在调试时,我在指定的行下面仍收到错误消息“ T不包含Id的定义 ”,尽管我看到“ item”在其基类 中确实具有属性“ Id”。

How do I specify here that I want C# to look in the item's base class for Id (and why doesn't it do this automatically?)? 如何在此处指定我希望C#在项目的基类中查找ID(为什么它不自动执行此操作?)?

//public abstract class Items<T> : ItemBase (causes same error)
public abstract class Items<T> where T : ItemBase
{
    public List<T> _collection = new List<T>();
    public List<T> Collection
    {
        get
        {
            return _collection;
        }
    }

    public int GetNextId()
    {
        int highestId = 0;
        foreach (T item in _collection)
        {
           //ERROR: "T does not contain a definition for Id
           if (item.Id > highestId) highestId = item.Id; 
        }

        return highestId;
    }

}

Here's how the classes are being defined: 这是定义类的方式:

public class SmartForm : Item
{
    public string IdCode { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int LabelWidth { get; set; }
    public int FormWidth { get; set; }
    ...


public abstract class Item : ItemBase
{
    public int Id { get; set; }
    public DateTime WhenCreated { get; set; }
    public string ItemOwner { get; set; }
    public string PublishStatus { get; set; }
    public int CorrectionOfId { get; set; }
    ...

Your problem is that there is no constraint on T, so, at compile time, all the compiler knows is that T is some sort of object. 您的问题是对T没有任何约束,因此,在编译时,所有编译器都知道T是某种对象。 If you know what type T will always inherit off, you can add a generic constraint to the class definition: 如果您知道T将始终继承什么类型,则可以向类定义添加通用约束:

public abstract class Items<T> : ItemBase where T : Item
{
//....
}

When you're debugging, T is instantiated to an Item (or subclass of) that does have an Id property, but the compiler doesn't know that at compile time, hence the error. 当您调试时,T被实例化为确实具有Id属性的Item(或其子类),但是编译器在编译时不知道该错误,因此会出错。

You got your generic constraint wrong. 您的通用约束有误。

public abstract class Items<T> : ItemBase

should be: 应该:

public abstract class Items<T> where T : ItemBase

What happened is that while your class has a collection of items, your ItemBase is not associated with T 发生的事情是,虽然您的班级有一系列的项目,但您的ItemBaseT没有关联

Because T could be absolutely anything, and C# is meant to be type-safe. 因为T绝对可以是任何东西,并且C#是类型安全的。 Use a constraint on T to a type which has an id property, and you should be OK. 在T上使用具有id属性的类型的约束,您应该可以。 See here for more info. 有关更多信息,请参见此处

您必须使用where子句指定T始终从ItemBase派生。

You accidentally let Items inherit ItemBase, instead of T inheriting ItemBase. 您不小心让Item继承了ItemBase,而不是T继承了ItemBase。 Just change 只是改变

: ItemBase

to

where T : ItemBase

this might help :-) 这可能有帮助:-)

   foreach (ItemBase item in _collection)
    {
       if (item.Id > highestId) highestId = (Item)item.Id; 
    }

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

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