C # returns various types from a function
I wrote the following code which basically makes a JSON request to my server:
var client = new RestClient("mysite.com");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("application/x-www-form-urlencoded", /*params here...*/ ,ParameterType.RequestBody);
return JsonConvert.DeserializeObject<MyCustomType>(client.Execute(request).Content);
Notice the last line of code:
return JsonConvert.DeserializeObject<MyCustomType>(client.Execute(request).Content);
Where I de-serialize the returned JSON from the server to the earlier class structures in my application.
Now the problem is that the server can either return an error response or a success response, and for each of these cases I need a different class structure so that I can map them to C # class objects ...
So I thought it would be like this:
public object PerformMyAwesomeRequest(string myToken)
{
var response =client.Execute(request).Content;
var statusCode = FetchStatusCode(response);
if(statusCode == 0)//signaling the response returned error
{
return JsonConvert.DeserializeObject<ErrorMessageCustomType>(response);
}
else{
return JsonConvert.DeserializeObject<MyCustomType>(response);
}
}
So it doesn't give me any errors ... But the problem here is that these objects now only exist at runtime, and I can't figure out which type server was returning in my .NET MVC Action, which looks like this:
public ActionResult SomeActon(){
var response = PerformMyAwesomeRequest("mytokenhere...");
//response object is assigned a corresponding type only at runtime...
}
So my question is:
- What type can I return from my function that is making an HTTP request to the server so that I can know what type was returned from the function directly in code, not just at runtime?
source to share
The only way for a method to return objects of different types is to return their common base type. However, you've already found that a generic superclass of a class object
won't help you, so let's consider alternatives to returning an object in the first place:
- Back to Success and Throwing an Error - This approach allows you to return
MyCustomType
and throw some packing of exceptionsErrorMessageCustomType
. Since one object is returned and the other is discarded, there is no need to bind the types. - Returns a merged object - create a class with members for each type you plan to return and only set one of them in the method
PerformMyAwesomeRequest
. This is not so good because the calling program requires the chainif
-else
. - Accept Parameter
out
for Error - If the request was not successful, returnnull
and set the parameterout
toErrorMessageCustomType
. - Accept callback delegates instead of returning a value, accept two delegates, one for success and one for failure scenario i.e.
void PerformMyAwesomeRequest(string tok, Action<MyCustomType> onSuccess, Action<ErrorMessageCustomType> onError)
...
The latter approach can be used like this:
PerformMyAwesomeRequest(
myToken
, data => {
Console.WriteLine("Received data: {0}", data);
}
, error => {
Console.WriteLine("Received error: {0}", error);
}
);
The method can be implemented as follows:
public void PerformMyAwesomeRequest(string myToken, Action<MyCustomType> onSuccess, Action<ErrorMessageCustomType> onError) {
var response = client.Execute(request).Content;
var statusCode = FetchStatusCode(response);
if (statusCode != 0) {
onSuccess(JsonConvert.DeserializeObject<MyCustomType>(response));
} else {
onError(JsonConvert.DeserializeObject<ErrorMessageCustomType>(response));
}
}
source to share