简体   繁体   English

是否可以在C#中的泛型类中获取派生类属性?

[英]Is it possible to get Derived class properties at generic class in c#?

I have a generic class and interface of base class type [Event]. 我有一个通用类和基类类型为[Event]的接口。 There are number of classes derived from this base class [Event]. 有许多从该基类[Event]派生的类。 I am able to get the properties of the base class[Event] in my generic class method. 我可以在通用类方法中获取基类[Event]的属性。 However, I am not able to get the derived class property. 但是,我无法获取派生类属性。 Few properties are same in all the derived classes and i would like to access those properties in my generic class method. 在所有派生类中,几乎没有属性是相同的,我想在我的通用类方法中访问那些属性。

Is there any way to do this. 有什么办法可以做到这一点。

Thanks 谢谢

public class Online<T> : IOnline<T> where T : Event

{

    public T message { get; set; }
    public void Process(IDomainContainer container, T message)
    {
      var Eventproperty= message.Eventproperty; // no error
       var derivedclassproperty= message.derivedclassproperty; //compile time error
    }

}

声明一个从Event派生的基类EventBase,添加派生类的公共属性,然后将Online更改为使用EventBase而不是Event。

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

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