4

I am trying to make a class in C# that can be used to return data of any types.

public class ResponseObject
{
    public <T> data { get;set }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}

The Object will be a wrapper for the response object when my application sends a request to the API.

i have tried researching this but cannot find any tutorials which are relevant to what i am trying to do.

Is this possible in C#? the Response Object will be converted to a JSON string and then sent as a response.

I will not be doing any processing of this object as that will already by done. I just want to place the data inside the ResponseObject and send it

I want to do something along the lines of:

var customers = repository.GetCustomers();
var orders = repository.GetOrders();
if(customers)
{
  success = true;
  message = "";
}
else{
   success = false;
   message = "failed to get customers";
}
if(orders)
{
  orderssuccess = true;
  ordersmessage = "";
}
else{
   orderssuccess = false;
   ordersmessage = "failed to get orders";
}
ResponseObject customerResponse = new ResponseObject{
    data = customers,
    success = success,
    message = message 
};
ResponseObject orderResponse = new ResponseObject{
    data = orders,
    success = orderssuccess,
    message = ordersmessage 
};
vmp
  • 271
  • 1
  • 3
  • 13
  • *"Is this possible in C#?"* - what exactly? Careful with phrases like *"cannot find any tutorials"* - asking for tutorial is offtopic. – Sinatr Jul 10 '18 at 09:49
  • You probably need to write a generic method called `GetData` instead. – Sweeper Jul 10 '18 at 09:50
  • 2
    Possible duplicate of [Making a generic property](https://stackoverflow.com/questions/271347/making-a-generic-property) – Cid Jul 10 '18 at 09:51
  • Cheers for all your responses. I typed up an example of what i want to do and had a load of replies when i refreshed – vmp Jul 10 '18 at 09:54

3 Answers3

8

You need to add <T> to the class and use T as the type of your property.

public class ResponseObject<T>
{
    public T data { get; set; }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}
Markus Dresch
  • 5,290
  • 3
  • 20
  • 40
7

You have almost done it already! Just change your <T> to T.

public class ResponseObject<T> where T : class
{
    public T data { get; set; }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}

Here where T : class ensure that the generic type parameter is a reference type. From your question it seems you are going to pass in an object there.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
2

You have two options here:

  1. Make the class generic, or
  2. Use generic methods for accessing the property

Here are the examples of both approaches:

// Make the class generic
public class ResponseObject<T> {
    public T Data { get; set }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}

// Use generic methods to access the property
public class ResponseObject {
    private object data;
    public T GetData<T>() {
        return (T)data;
    }
    public void SetData<T>(T newData) {
        data = newData;
    }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}

Second approach is less robust than the first one - basically, it's a glorified unrestricted cast. First approach, however, does not let you build a container of ResponseObjects with different Ts. You can address this problem by adding an interface on top of ResponseObject:

interface IResponseObject {
    object DataObj { get; set }
    Type ObjType { get; }
    bool Success { get; set; }
    string Message { get; set; } 
}
public class ResponseObject<T> {
    public T Data { get; set }
    public ObjType => typeof(T);
    public DataObj {
        get => Data;
        set => Data = value; // C# 7 syntax
    }
    public Boolean Success { get; set; }
    public string Message { get; set; } 
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523