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 }; 
4
  • "Is this possible in C#?" - what exactly? Careful with phrases like "cannot find any tutorials" - asking for tutorial is offtopic. Commented Jul 10, 2018 at 9:49
  • You probably need to write a generic method called GetData instead. Commented Jul 10, 2018 at 9:50
  • 2
    Possible duplicate of Making a generic property Commented Jul 10, 2018 at 9: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 Commented Jul 10, 2018 at 9:54

3 Answers 3

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; } } 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

3 Comments

What exactly is the reason for the SetData and GetData methods, when we can do that with just the property? Just curious
@Sнаđошƒаӽ The reason is exactly that you cannot do it with just the property without making the class generic (think of a collection of ResponseObjects with different Ts).
Didn't think of ResponseObjects with different Ts. So yeah, in such cases yours is the way to do it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.