简体   繁体   中英

Reference different kind of objects in a class

I'm in the process of creating the Model (MVC) for a new front end in ASP.NET MVC 4.

I'm using an existing backend, that when called returns status codes and the data.

I've created a Class "Status" which holds the status codes, but I'm not sure of the correct procedure to append or attach different kind of Objects to the "Status" Class.

For now i just have an Object called data to hold whatever data the backend returns, but it seems to me that there should be a better way to do this?

public class Status
{
    public string SeverityCode { set; get; }
    public double ReturnCode { set; get; }
    public double ReasonCode { set; get; }
    public Object data { get; set; }
}

You can leave it as is and leave processing to the class that will consume your Status object, or you can subclass it to a generic Status<TData> , where the TData is the type of additional data:

public abstract class Status
{
    public string SeverityCode { set; get; }
    public double ReturnCode { set; get; }
    public double ReasonCode { set; get; }
    protected object Data { get; set; }
}


public class Status<TData>: Status where TData: class
{
    public new TData Data { get { return (TData)base.Data; } set { base.Data = value; } }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM