Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:31:23+00:00 2026-05-24T04:31:23+00:00

This is my first time using StackOverflow myself. I have found many answers to

  • 0

This is my first time using StackOverflow myself. I have found many answers to many of my question here before, and so I thought I would try asking something myself.

I’m working on a small project and I am a bit stuck right now. I know ways to solve my problem – just not the way I want it to be solved.

The project includes an NBT parser which I have decided to write myself since it will be used for a more or less custom variation of NBT files, though the core principle is the same: a stream of binary data with predefined “keywords” for specific kinds of tags. I have decided to try and make one class only for all the different types of tags since the structure of the tags are very similar – they all contain a type and a payload. And this is where I am stuck. I want the payload to have a specific type that, when an explicit conversion is done implicitly, throws an error.

The best I could come up with is to make the payload of type Object or dynamic but that will allow all conversions done implicitly:

Int64 L = 90000;
Int16 S = 90;
dynamic Payload; // Whatever is assigned to this next will be accepted
Payload = L; // This fine
Payload = S; // Still fine, a short can be implicitly converted to a long
Payload = "test"; // I want it to throw an exception here because the value assigned to Payload cannot be implicitly cast to Int64 (explicit casting is ok)

Is there any way of doing this? I would like to solve it by somehow telling C# that from now on, even though Payload is dynamic, it will throw an exception if the assigned value cannot be implicitly converted to the type of the current value – unless, of course, it is done explicitly.

I am open to other ways of accomplishing this, but I would like to avoid something like this:

public dynamic Payload
{
    set
    {
        if(value is ... && Payload is ...) { // Using value.GetType() and Payload.GetType() doesn't make any difference for me, it's still ugly
            ... // this is ok
        } else if(...) {
            ... // this is not ok, throw an exception
        }
        ... ... ...
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:31:24+00:00Added an answer on May 24, 2026 at 4:31 am

    Have you considered using generics?
    This would automatically give you compile-time checking on which conversions are allowed.

    class GenericTag<T>
    {
        public GenericTag(T payload)
        {
            this.Payload = payload;
        }
    
        public T Payload { set; get; }
    }
    
    // OK: no conversion required.
    var tag2 = new GenericTag<Int64>(Int64.MaxValue);
    
    // OK: implicit conversion takes place.
    var tag1 = new GenericTag<Int64>(Int32.MaxValue);
    
    // Compile error: cannot convert from long to int.
    var tag4 = new GenericTag<Int32>(Int64.MaxValue);
    
    // Compile error: cannot convert from string to long.
    var tag3 = new GenericTag<Int64>("foo");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the first time I am posting a question on stackoverflow, so please
This is my first time using JQuery in any of my projects. I have
First time posting here after having great results searching for other answers on stackoverflow.
First stackoverflow question! I've searched...I promise. I haven't found any answers to my predicament.
Well this is not the first time im asking these question here but I
This is my first time using joomla. I don't know if I'm using the
This is my first time with Web services. I have to develop web services
This is a really basic question but this is the first time I've used
I have recently started learning F#, and this is the first time I've ever
My apologies if this question is too general, as this is my first time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.