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 8501801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:18:31+00:00 2026-06-11T01:18:31+00:00

I’m working on a rather tricky problem. At the very high level, I have

  • 0

I’m working on a rather tricky problem. At the very high level, I have an object which is a primitive and I need to coerce it into a different primitive type in C#. The values and the types are determined at runtime.

I tried roughly this (my code is more complex, this demonstrates the problem):

object value = (int)0x8ba9dc90;
Type t = typeof(UInt64);
object result = Convert.ChangeType(value, t);

This works some of the time, except in the case (as above) where an overflow or underflow would occur.

What I want to happen is that coercion (instead of conversion) would occur. In this case, I’d simply like that “(int)0x8ba9dc90” to be “(ulong)0x8ba9dc90”. Similar with floats: if value = “(float)-32.01” and “t” is “UInt64”, I want the result to be “0xffffffffffffffe0”. Which is exactly what you get when you run “unchecked { ulong u = (ulong)(double)-32.01; }”

Is there any way to do this, or am I stuck writing a custom converter?

(Yes, I realize this is an odd thing to try to do. This is all highly dynamic code, and I’m trying to do coercion in a DynamicObject.TryConvert override. I also am fully aware there are a lot of cases this will throw away data through down casts, etc. That’s perfectly fine in my application. I just can’t figure out how to write this without a giant nested switch statement.)

EDIT: To be clear my function looks something like this:

public override bool TryConvert(ConvertBinder binder, out object result)
{
    if (binder.Type.IsPrimitive && m_type.IsPrimitive)
    {
        // m_type is System.Type, which is m_value's current type.
        // m_value is System.Object, contains a primitive value.
        // binder.Type is the System.Type I need to coerce m_value into.
        result = Convert.ChangeType(m_value, binder.Type);
        return true;
    }
    result = null;
    return false;
}
  • 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-06-11T01:18:33+00:00Added an answer on June 11, 2026 at 1:18 am

    You can use LINQ expressions to do this conversion. This article explains how.

    The basic idea is to construct a LINQ expression equivalent to a cast expression.

    ParameterExpression convParameter = Expression.Parameter(typeof(object), "val");
    var conv = (Func<object,object>)Expression.Lambda(
        Expression.Convert(
            Expression.Convert(
                Expression.Convert(
                    convParameter
                ,   fromType
                )
            ,   targetType
            )
        ,   typeof(object)
        )
        ,   convParameter
    ).Compile();
    

    Now you can call conv and pass it an object wrapping a value of the original type; it will return a coerced value back to you. This code will pick up your custom type conversions too (for your own types, not for primitives). If necessary, you can add more conversions in between by increasing the level of nesting of your Expression.Convert objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have an array which has BIG numbers and small numbers in it. I

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.