简体   繁体   English

访问从通用抽象库派生的类中的重写的抽象属性所需的接口?

[英]Interface needed to access overridden abstract property in classes derived from generic abstract base?

So my dilemma is that in order to access IntThing 's or StringThing 's MyProperty from UtilityThing<T> , I'm defining an interface with MyProperty and using it as the generic constraint on T in UtilityThing<T> . 所以我的困境是,为了从IntThing UtilityThing<T>访问IntThingStringThingMyProperty ,我定义了MyProperty接口,并将其用作UtilityThing<T>T的一般约束。 This is working, but seems redundant given that the same property is already defined in the abstract base. 这是可行的,但是鉴于抽象库中已经定义了相同的属性,因此似乎是多余的。 Am I missing a facet of design here, or is this actually the way it needs to be done in this instance? 我是否在这里缺少设计方面,或者这实际上是在这种情况下需要采用的方式?

public interface IThing {
    string MyProperty { get; set; } 
}

public abstract class Thing<T> {
    protected string _MyProperty;
    public abstract string MyProperty { get; set; }
    public T OtherProperty { get; set; }

    public string CommonMethod() {
        return MyProperty + "foobar";   
    }
}

public class IntThing : Thing<int?>, IThing {
    public override string MyProperty {
        get { return _MyProperty; }
        set { _MyProperty = value + OtherProperty.ToString(); }
    }
}

public class StringThing: Thing<string>, IThing {
    public override string MyProperty {
        get { return _MyProperty; }
        set { _MyProperty = OtherProperty + value; }
    }
}

public class UtilityThing<T> where T: IThing, new() {
    public T DoIt(SomeContext someContext, string name) {
        string contextVal = someContext.GetValue(name);

        var thing = new T { MyProperty = contextVal }
        return thing;
    }
}

You'll need to introduce a new generic type. 您需要引入一个新的泛型类型。 Once the new type is introduced you can eliminate the need of the interface. 引入新类型后,您就可以消除对接口的需求。

public class UtilityThing<T, I> where T : Thing<I>, new()
{
    public T DoIt(SomeContext someContext, string name)
    {
        string contextVal = someContext.GetValue(name);

        var thing = new T { MyProperty = contextVal };
        return thing;
    }
}

And you can use it like this: 您可以像这样使用它:

var utility = new UtilityThing<IntThing, int?>();

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

相关问题 实现接口的抽象基类 - Abstract base classes that implement an interface 如何在非抽象基类的派生类中强制重写? - How to force overrides in a derived class from non abstract base classes? null抽象基类/派生类的合并问题 - null coalescing issue with abstract base/derived classes 有没有更好的方法从其抽象基类调用派生类的重写方法? - Is there a better way to call a derived class's overridden method from its abstract base class? 为什么派生类不能使用扩展该接口的类型覆盖基类的接口类型抽象属性? - Why can't a derived class override base class' interface type abstract property with a type that extends that interface? 如何为c#中的泛型派生类实现抽象非泛型基类? - How to implement a abstract nongeneric base class, for generic derived classes in c#? c#,从抽象工厂类获取泛型类型属性 - c# ,Get Generic Type Property from Abstract Factory Classes 将方法上的泛型类型约束为从抽象泛型基类的任何变体形式派生的任何类 - Constrain generic type on a method to be any class that is derived from any variant form of an abstract generic base class 多个派生抽象类? - Multiple derived abstract classes? 检查类型是否派生自抽象泛型类 - Check if type is derived from abstract generic class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM