Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on api project to return different response such as string, list, list<t> object ..etc.
To organize my response I add a class to generate the response message to be easy to used.

What I have tried:

C++
public class ResponseMessage {

        public string responseStatus { get; set; }
        public string responseMessage { get; set; }
        public IList responseDetail { get; set; }

}


In my case responseDetail should be dynamic and filled with different types. What is the best practice to do something like with low effort of code ?

Is there any article to explain what I should to do in this case ?

Thanks
Posted
Updated 15-Jul-19 22:54pm

If there is nothing that the target types have in common you'll need to use

List<object>
 
Share this answer
 
I would create an abstract class (or Interface) which represents the "base type" for objects that can be included as a responseDetail: derive all the types you need (or your API user needs) from that, and allow them as your type:
C#
public abstract class ResponseDetail {}
public class ResponseMessage {
        public string responseStatus { get; set; }
        public string responseMessage { get; set; }
        public ResponseDetail responseDetail { get; set; }
}

C#
public class ResponseDetailList : ResponseDetail
   {
   public List<string> Texts = new List<string>
   }

That way, it's expandandable, but still typesafe.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900