简体   繁体   中英

Accessing base class method using generic T

I have this base class:

public class BaseEvent
{
    public int EventID { get; set; }

    public int GetEventID()
    {
        return EventID;
    }
}

And then I have another class inherited from that base one:

public class ValidationResult<T> where T : BaseEvent
{
    private void AddEventStatusUpdater(ValidationResult<T> validationResult)
    {
        validationResult.GetEventID();
    }
}

The issue I´m having is that I cannot access the GetEventID() method from the base class.

I think this may happen because I´m using a T generic. Is there any other way to access this method?

public class ValidationResult<T> where T : BaseEvent

Says that T must be a BaseEvent , not that ValidationResult<T> inherits from BaseEvent . That'd be:

public class ValidationResult<T> : BaseEvent

And there T would not have any constraint

Is that what you want?

You have begun to implement a generic interface rather than inheriting from the base class. I think you meant to do the following:

public class ValidationResult : BaseEvent
{
    private void AddEventStatusUpdater()
    {
        var id = this.GetEventID();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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