如何创建一个抽象构造函数或获取抽象类的类型并返回它



我无法使抽象类中的方法或构造函数正常工作。

基本上有几个数据协定类来扩展我的抽象类,并且其中有一个简单、几乎相同的方法,我试图弄清楚如何移动到我的抽象类。

我很确定构造函数最有意义,但我无法找出正确的语法。为了目前使用它,我称之为这样的东西:

OrderLine orderLine = new OrderLine();

orderLine = orderLine.createFromJsonString("MyJsonString");

我正在尝试将标记为 (1) 和 (2) 的方法作为方法或构造函数移动到 (0) 位置。

abstract class Pagination<T>
{
    public int _offset { get; set; }
    public int _total { get; set; }
    public string previous { get; set; }
    public string next { get; set; }
    public abstract List<T> items { get; set; }
    public int getItemCount()
    {
        return items != null ? items.Count() : 0;
    }
    // (0)
    // Each of the child objects that extend this class are created from
    // a Json that is deserialized. So I'd rather some method that would
    // construct or return a new instance of the abstract object 
    /*
    public object createFromJsonString(string _json)
    {
     * // The main issue here is the "this" keyword
        return JsonConvert.DeserializeObject<this>(_json);
    }
     **/
    
}
class OrderHeader : Pagination<OrderLine>
{
    public int orderId { get; set; }
    public List<OrderLine> items { get; set; }
    // (1)
    // How can I move this into the abstract class?
    // Or should it be written as constructor?
    public OrderHeader createFromJsonString(string _json)
    {
        return JsonConvert.DeserializeObject<OrderHeader>(_json);
    }
}
class OrderLine : Pagination<OrderLineDetails>
{
    public string sku { get; set; }
    public int qty { get; set; }
    public List<OrderLineDetails> items { get; set; }
    // (2)
    // How can I move this into the abstract class?
    // Or should it be written as constructor?
    public OrderLine createFromJsonString(string _json)
    {
        return JsonConvert.DeserializeObject<OrderLine>(_json);
    }
}
class OrderLineDetails
{
    public string serialNum { get; set; }
}

在这里,您做错了几件事:

// You have created object once here, this object would become unused in next line
OrderLine orderLine = new OrderLine();
// Here you are building a new object via Deserialize
orderLine = orderLine.createFromJsonString("MyJsonString");

我从您的问题中了解到的是,您希望有一个工厂方法来创建派生类型的分页<>的对象。

abstract class Pagination<T>
{
    public int _offset { get; set; }
    public int _total { get; set; }
    public string previous { get; set; }
    public string next { get; set; }
    public abstract List<T> items { get; set; }
    public int getItemCount()
    {
        return items != null ? items.Count() : 0;
    }
    /// <summary>
    /// Factory method to build the pagination object from Json string.
    /// </summary>
    public static TCurrent CreateFromJsonString<TCurrent>(string _json) where TCurrent: Pagination<T>
    {
        return JsonConvert.DeserializeObject<TCurrent>(_json);
    }
}

现在,您可以构建派生类型的对象,例如:

 OrderHeader hdr = Pagination<OrderLine>.CreateFromJsonString<OrderHeader>(json);
 OrderLine line = Pagination<OrderLineDetails>.CreateFromJsonString<OrderLine>(json);

Factory 方法还防止执行以下操作,因为我们应用了泛型约束,因此只允许相关的项类型。

// This will throw error of invalid implicit conversion
OrderHeader invalidObj = Pagination<OrderLineDetails>.CreateFromJsonString<OrderHeader>(json);

相关内容

  • 没有找到相关文章

最新更新