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?
See Question&Answers more detail:os