I have a slightly strange problem here. I have a limit on the draft, where the property value must be either a number ( int, double, long, and so on are all acceptable), a stringor datetime. The reason is that the Value parameter must be one of these three (err..well, if you consider all the possible numerical value of the types, allow it a little more) Types - this is because, depending on the type, the base value will need to be converted to special formats for serialization in the REST API. To simplify, here is the basic idea of ββa class like POCO:
class Foo
{
public string Name {get;set;}
public Guid Id {get; set;}
public UNKNOWN Value {get;set;}
}
I was thinking about using generics for this, with a limitation where T : struct, but it still leaves too many types that could theoretically be set, which are actually invalid. Of course, I can perform type checking and throw exceptions during the construction / installation of the Value parameter, but this does not look like a "clean" code.
I examined this question. How do you work with a variable that can be of several types? but this did not help, since it was more of a question of inheritance. However, using multiple fields with a null value and returning one property based on which was populated is an opportunity, but again I believe that there should be a better way.
, , - dynamic ( / ). , .
- ? , , ?