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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:28:34+00:00 2026-06-04T05:28:34+00:00

I have a class that pretends to be an int, so it has overloaded

  • 0

I have a class that pretends to be an int, so it has overloaded the various operators;

public class MyId
{
    int value;
    public virtual int Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    public MyId(int value)
    {
        this.value = value;
    }


    public static implicit operator MyId(int rhs)
    {
        return new MyId(rhs);
    }

    public static implicit operator int(MyId rhs)
    {
        return rhs.Value;
    }


}

However, when I use code like

PropertyInfo.SetValue(myObj, 13, null)
OR
MyId myId = 13;
int x = Convert.ToInt32(myId);
IConvertible iConvertible = x as IConvertible;
iConvertible.ToType(typeof(MyId), CultureInfo.CurrentCulture);

I get invalid cast. I’m puzzled, both calls seem to attempt to call convert on the int which will fail because int doesn’t understand the type MyId (even though all the assignment operators are there). Any ideas of a workaround for this, I’m sure I must be missing something stupid?

  • 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-04T05:28:36+00:00Added an answer on June 4, 2026 at 5:28 am

    Implicit conversions are a C# construct and are not available through reflection. Additionally, setting a field or property through reflection means that you must provide the appropriate type up front. You can attempt to circumvent this by using a custom TypeConverter (or some other means of custom conversion) to help convert your types at runtime prior to using reflection. Here’s a rough example of a TypeConverter implementation.

    public class MyIdTypeConverter : TypeConverter
    {                
        public override object ConvertFrom(ITypeDescriptorContext context,
                                           System.Globalization.CultureInfo culture,
                                           object value)
        {   
            if (value is int)
                return new MyId((int)value);
            else if (value is MyId)
                return value;
            return base.ConvertFrom(context, culture, value);
        }               
    }
    

    Here’s the type that we would be trying to set the Custom property on.

    public class Container
    {
        [TypeConverter(typeof(MyIdTypeConverter))]
        public MyId Custom { get; set; }                
    }
    

    The code to call it would have to check the attribute and perform the conversion ahead of time, after which it could call SetValue.

    var instance = new Container();
    var type = typeof(Container);
    var property = type.GetProperty("Custom");
    
    var descriptor = TypeDescriptor.GetProperties(instance)["Custom"];
    var converter = descriptor.Converter;                
    property.SetValue(instance, converter.ConvertFrom(15), null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class A such that: class A { static int i; A(); f1();
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class that extends Activity. That has a Logout menu options. When I
I have class that represents users. Users are divided into two groups with different
I have a class that implements the IDisposable interface. I am using a webclient
I have a class that is marked with DataContract attributes and I would like
I have a class that requires a specific method to be called before being
I have a class that maps incoming messages to matching readers based on the
I have a class that I am using below. And I am using this
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but

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.