❔ How to cast to a generic of object?
I am using an external api that returns dynamic data. It could be a string, a list or a table.
In my code, I am building a controller endpoint that will read the data from this API, parse the data from the body, and return as a result to the requester.
To help me with that, I have attempted to build a generic ApiResult<T> that will contain the parsed data. The issue is that I am attempting to cast the ApiResult<T> to an ApiResult<object> and unfortunately it doesn't allow me to do that since object and T doesn't seem to be compatible.
Could anyone help me? I don't need the result to be strongly typed, but I really wanted to have the Parse methods strongly typed to have intellisense.
4 Replies
Why do you need to cast ApiResult<T> to an ApiResult<object> ? Once you for example call ParseList method, it returns ApiResult<List<string>> which is already strongly typed
@Atakan / Cracker because I am gonna call ParseApiResult from a method that knows nothing about the underlying types. So it needs to get a generic ApiResult as a result
so, if you have, say
and you want to cast it to
ApiResult<object>
?
the only way that is possible is with polymorphic covariance
C# has this built-in for certain common types
like IEnumerable<T>
IEnumerable<T>
is defined as....
the key is the out
modifier on T
this is only possible within an interface
you could not add this to your ApiResult<T>
class
you'd have to define an IApiResult<T>
interface to be used for passing these things around
MVC does this, for example
alternatively
if your goal is to write the ParseApiResult()
method you describe, you could do something like this
not ideal, but doable
alternatively alternatively, you'd need to use some reflection in your parse method
there MIGHT be an even better solution than any of these, but I'd need to know more context about what you're trying to achieve, overall, with this ApiResult<T> typeWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.