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

时间:2016-02-10 21:28:14

标签: c# .net constructor json.net abstract-class

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

我基本上有几个数据契约类,它们扩展了我的抽象类,并且在它们内部有一个简单的,几乎相同的方法,我正试图弄清楚如何移动到我的抽象类。

我很确定构造函数最有意义,但我无法弄清楚正确的语法。目前使用它,我称之为:

  

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; }
}

1 个答案:

答案 0 :(得分:2)

这里你做错了一些事情:

// 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");

我从您的问题中了解到,您希望使用工厂方法来创建衍生类型的分页&lt;&gt;的对象。

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);
    }
}

现在您可以构建Derived类型的对象,如:

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

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

工厂方法也会阻止执行类似下面的操作,因为我们已经应用了通用约束,因此只允许相关的项类型。

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