访问派生类属性成员 [英] Accessing derived class property members

查看:161
本文介绍了访问派生类属性成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组业务逻辑类继承自一系列抽象类。我也有一组业务数据类继承自己自己的抽象类链。在这两种情况下,我的派生类(C)继承自一个抽象类(B),它本身继承自一个抽象类(A)。 (因此,C:B和B:A ...注意,这个约定是为了演示问题,而不是我的程序中使用的名称)。



我的逻辑类链表现得如预期。方法调用使用正确的基础或重写的实现。对于我的数据类(新字段/属性出现在正确的级别)也是如此。



我的问题是试图添加数据类作为逻辑类的属性。基本抽象逻辑类(A)必须能够调用基本抽象数据类(再次,A)中的一些字段。然而,链中的下一个抽象逻辑类(B)将需要在链的下一级(B)中使用一些字段/属性。对于派生的具体类型(C)也是如此。



现在,我最初假设如果在我的A逻辑类中为基本抽象数据类添加一个属性,我可以使用这个属性例如在B逻辑类中,我可以使用this.Data.Variable来访问变量字段,从而变量字段在B数据类中实现。我发现没有一个cast,我被限制在整个逻辑类链中使用基础抽象数据类中暴露的字段。



我发现了一些好的建议这个网站,并沿着使用泛型的路径。我仍然需要在基本逻辑类中执行转换,当我使用数据类上的字段时(但在使用派生类中的字段时不需要转换)。



看下面我的类的样本。我已经在基本逻辑类(LogicA)中实现了一个MethodA,以演示我需要转换为正确的数据类型以使用它的字段。

  abstract class LogicA< T> 
{
public T Data {get;组; }

public virtual void MethodA()
{
var data =(DataA)((dynamic)this.Data);
data.FieldA =T;
}
}


抽象类LogicB< T> :LogicA< T>
{
public virtual void MethodB()
{

}
}

class LogicC:LogicB< DataC&
{
public void MethodC()
{
this.Data.FieldB =BBB;
}
}

抽象类DataA
{
public string FieldA {get;组; }
}

抽象类DataB:DataA
{
public string FieldB {get;组; }
}

class DataC:DataB
{
public string FieldC {get;组; }
}

当使用逻辑类时,我可以执行以下操作,

  LogicC logic = new LogicC(); 
logic.Data = new DataC();
logic.Data.FieldA =A;
logic.Data.FieldB =B;
logic.Data.FieldC =C;
logic.MethodA();

我目前关心的是有一个更好的方法,实现?我可以看到这种方法在使用数据字段时通过抽象逻辑类进行转换是乏味的。

解决方案

您的逻辑类:

 抽象类LogicA< T& 
其中T:DataA
{
public T Data {get;组; }

public virtual void MethodA()
{
var data =(DataA)((dynamic)this.Data);
data.FieldA =T;
}
}

对您的hierarcy中的其他类使用相同的方法。 / p>

I have a set of business logic classes that inherit from a chain of abstract classes. I also have a set of business data classes that inherit from their own chain of abstract classes. In both cases my derived class (C) inherits from an abstract class (B), which itself inherits from an abstract class (A). (Therefore, C : B and B : A ... note that this convention is to demonstrate the problem and not the names used in my program).

In isolation, my chain of logic classes behave as expected. Method calls use the correct base or overridden implementations. The same goes for my data classes (new fields/properties appear at the correct level).

My problem is trying to add the data classes as a property of the logic classes. The base abstract logic class (A) must be able to call upon some fields in the base abstract data class (again, A). However, the next abstract logic class in the chain (B) will need to use some fields/properties in the next level of the chain (B). The same for the derived concrete types (C).

Now, initially I assumed that if in my "A" logic class I add a property for the base abstract data class, I could use this property further up the chain (e.g. in the "B" logic class I could use this.Data.Variable to access the "Variable" field, whereby the "Variable" field is implemented in the "B" data class). I found that without a cast, I was restricted to using the fields exposed in the base abstract data class throughout the entire logic class chain.

I have found some good advice on this site, and went down the path of using generics. I still need to perform a cast in the base logic classes when I use fields on the data class (but do not need to cast when using fields in the derived class).

See below for a sample of my classes. I have implemented a MethodA in the base logic class (LogicA) to demonstrate where I need to cast to the correct data type to use its fields.

abstract class LogicA<T>
{
    public T Data { get; set; }

    public virtual void MethodA()
    {
        var data = (DataA)((dynamic)this.Data);
        data.FieldA = "T";
    }
}


abstract class LogicB<T> : LogicA<T>
{
    public virtual void MethodB()
    {

    }
}

class LogicC : LogicB<DataC>
{
    public void MethodC()
    {
        this.Data.FieldB = "BBB";
    }
}

abstract class DataA
{
    public string FieldA { get; set; }
}

abstract class DataB : DataA
{
    public string FieldB { get; set; }
}

class DataC : DataB
{
    public string FieldC { get; set; }
}

When using the logic class, I can perform the following without any issues:

        LogicC logic = new LogicC();
        logic.Data = new DataC();
        logic.Data.FieldA = "A";
        logic.Data.FieldB = "B";
        logic.Data.FieldC = "C";
        logic.MethodA();

My current concern is basically... is there a better way to pull off what I am trying to achieve? I can see this approach being tedious with the casting through the abstract logic classes when using the data fields.

解决方案

Just specify restriction for your logic class:

abstract class LogicA<T>
    where T: DataA
{
    public T Data { get; set; }

    public virtual void MethodA()
    {
        var data = (DataA)((dynamic)this.Data);
        data.FieldA = "T";
    }
}

Use same approach to other classes in your hierarcy.

这篇关于访问派生类属性成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆