如何添加不同类的通用属性< T>在现有类的内部< T> [英] How to add a generic property of different class<T> inside of an existing class<T>

查看:103
本文介绍了如何添加不同类的通用属性< T>在现有类的内部< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定如何在单行中解释它,但这是我的情况。



我有一个名为Deal的抽象基类,它有一个属性在它的类库存



现在交易可以是现金,财务,租赁,批发或租赁自己这就是为什么它的原因输入交易。它也是抽象的,因为每种交易类型都有一些不适用于其他交易的属性,但它们都有一组共享,因此各种交易类型(即 - 财务,租赁等)继承自基类交易并拥有他们自己添加的方法和属性。



另一个抽象类库存也是如此。库存可以是汽车,卡车,船,房车等等。所以我有一类库存。



这是问题所在:



交易必须包含库存中的单位。例如,您为汽车提供资金或者租赁房车,然后您就可以处理库存类型或任何情况下的库存属性。



当我创建时Deal类和我像SoldUnit一样添加一个属性作为Inventory类型,T继承自Deal类。如何在Deal类中创建Inventory类型的通用属性?



由于可能性的多少,交易将无法正常工作,所以我怎么能做一个通用的SoldUnit属性,它引用Deal类中的Inventory类?在保持严格打字的同时?我知道我可以使用一种对象或动态创建一个名为SoldUnit的属性,但是有更好的方法吗?



这里是代码的摘录:它没有不行,因为T型已经是DealType,但它是两个属性[ret.Vehicle = new Car();和ret.SoldUnit = new Car();我试图在Get()方法中填充特定的库存类型(汽车,卡车,rv,船等)。特定库存类型由数据库字段中的标志确定。



Not really sure how to explain it in a one-liner but here's my situation.

I have an abstract base class called "Deal" which has a property in it which is of a class "Inventory"

Now a deal can be either Cash, Finance, Lease, Wholesale or Rent-To-Own which is why it's of type Deal. It's also abstract as each of the deal types have some properties that don't apply to other but they all have a set of shared so the various deal types (i.e. - Finance, Lease, etc.) inherit from the base class Deal and have their own added methods and properties.

The same is true with another abstract class of "Inventory". Inventory can be things like car, truck, boat, RV, etc.) so therefore I have a class of Inventory.

Here's the issue:

A deal must contain a unit in inventory. For example, you finance a car or you lease an RV then you have a Deal with an inventory property of type Inventory or whatever the case may be.

When I create the Deal class and I add a property to it like SoldUnit as type Inventory, the T is inherited from the Deal class. How can I create a generic property of type Inventory inside of the Deal class?

Deal won't really work because of the number of possibilities, so how could I make a generic "SoldUnit" property which references an Inventory class inside of the Deal class? And while maintaining strict typing? I know I could make a property called SoldUnit with a type of object or dynamic, but is there a better way?

Here is an excerpt of the code: It doesn't work because type T is already a DealType, but it's the two properties [ ret.Vehicle = new Car(); and ret.SoldUnit = new Car(); ] that I'm trying to populate with a specific inventory type (car, truck, rv, boat, etc.) in the Get() method. The specific inventory type is determined by a flag in the database field.

public abstract class Deal<T>
{
    public string Uid { get; set; } = Guid.NewGuid().ToString();
    public DealTypes DealType { get; set; } = DealTypes.Finance;

    public Buyer Customer { get; set; }
    public Inventory<T> Vehicle { get; set; }
    public Location Location { get; set; }
    public Inventory<T> SoldUnit { get; set; }

    public abstract T Add(string id);
    public abstract T Update(string id);
    public abstract T Get(string id);
}

public abstract class Inventory<T>
{
    public string Uid { get; internal set; } = Guid.NewGuid().ToString();
    public Location Location { get; set; }
    public string StockNumber { get; set; } = "";
}

public class Cash : Deal<Cash>
{
    public decimal TradeAllowance { get; set; }
    public decimal TradePayoff { get; set; }
    public decimal TotalDown { get; set; }
    public decimal DueOnDelivery { get; set; }

    public override Cash Get(string id)
    {
        var ret = new Cash();
        ret.Customer = new Buyer();
        ret.Vehicle = new Car();
        ret.Location = new Location();
        ret.SoldUnit = new Car();
   }
}

public class Car : Inventory<Car>
{
    public decimal GrossWeight { get; set; }
    public decimal NetWeight { get; set; }
    public string Branded { get; set; } = "";
    public string FuelType { get; set; } = "";
    public string Trim { get; set; } = "";
    public string Body { get; set; } = "";
    public int Cylinders { get; set; }
    public decimal EngineSize { get; set; }
    public string TransmissionType { get; set; } = "";
    public string Suspension { get; set; } = "";
    public string InteriorColor { get; set; } = "";

    public bool OdometerExceedsLimit { get; set; }
    public bool OdometerTrueMileageUnknown { get; set; }
    public bool OdometerFiveDigit { get; set; }


    public string TitleNumber { get; set; } = "";
    public DateTime? TitleDate { get; set; }
    public string TitleState { get; set; } = "";
    public DateTime? TitleDue { get; set; }
    public DateTime? TitleReceived { get; set; }
}





有没有办法做到这一点?



谢谢!



我尝试过:



我试过用两种类型的



Is there a way to do this?

Thanks!

What I have tried:

I've tried making the Deal class with two types

public class Deal<TDeal, TInventory>

来制作Deal类,但是我必须为每种可能的交易组合制作一个类/库存类型。到目前为止,我能想到的唯一可行的方法是使类型为对象或动态的Vehicle和SoldUnit属性,但是我没有得到强类型或智能感知。



but then I would have to make a class for every possible combination of deal / inventory type. So far, the only thing I can think of that would work is to make the Vehicle and SoldUnit properties of type object or dynamic but then I don't get the strong typing or the intellisense.

public dynamic Vehicle { get; set; }




public object SoldUnit{ get; set; }

推荐答案

但Deal和Inventory都是抽象类,因此您无法创建其中任何一个。你可以让Deal成为一个真正的类,并使用枚举来决定它是什么类型;与库存相同。并且你不能做类似的事情

But both Deal and Inventory are abstract classes so you cannot create either of them. You could make Deal a real class and use an enumeration to decide which type it is; same with Inventory. And you cannot do something like
public class Car : Inventory<Car>



你不能创建一个类型还没有被定义。



我认为你已经过度了解你的课程。


You cannot create a type that has not yet been defined.

I think you are over engineering your classes.


这篇关于如何添加不同类的通用属性&lt; T&gt;在现有类的内部&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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