This is my first time I used StackOverflow. I have found many answers to many of my questions here before, and so I thought that I would try to ask something myself.
I am working on a small project and now I am a bit stuck. I know how to solve my problem - just not the way I want it to be solved.
The project includes an NBT analyzer, which I decided to write myself, since it will be used for more or less custom NBT file variants, although the basic principle is the same: a binary data stream with predefined "keywords" for certain types of tags. I decided to try to make one class only for all types of tags, since the structure of the tags is very similar - they all contain type and payload. And here I am stuck. I want the payload to have a specific type, which, when an explicit conversion is done implicitly, throws an error.
The best I can come up with is to do a payload like Object or dynamic, but that will allow all the conversions to be done implicitly:
Int64 L = 90000; Int16 S = 90; dynamic Payload;
Is there any way to do this? I would like to solve this, somehow tell C # that from now on, although Payload is dynamic, it throws an exception if the assigned value cannot be implicitly converted to the type of the current value - unless, of course, this is done explicitly.
I am open to other ways of doing this, but I would like to avoid something like this:
public dynamic Payload { set { if(value is ... && Payload is ...) {
source share