P
Peter
Hello
i use a simple class (or struct) with explicit cast operator:
public class UserNumber
{
private short value;
public UserNumber(short value)
{
this.value = value;
}
public static explicit operator UserNumber(short value)
{
return new UserNumber(value);
}
public static explicit operator short(UserNumber item)
{
return item.value;
}
}
Following code works as expected:
.....
UserNumber b = new UserNumber(5);
short test = (short)b;
-------------------
BUT this one throws Specified cast is not valid.
.....
UserNumber b = new UserNumber(5);
TestMethod(b);
.....
private void TestMethod(object b)
<-- Param is object
{
short test;
test =
(short)b;
<-- Exception
}
=======================================
short s = 5;
TestMethod(s);
Works fine.
Why ?
I use an existing dll that I can not change (as in Example
TestMethod(object value))
I suspect this Method cast all base types (if value.GetType() ==
typeof(short) .....
but of course not UserNumber
What can i do ? I must use TestMethod
Thanks
i use a simple class (or struct) with explicit cast operator:
public class UserNumber
{
private short value;
public UserNumber(short value)
{
this.value = value;
}
public static explicit operator UserNumber(short value)
{
return new UserNumber(value);
}
public static explicit operator short(UserNumber item)
{
return item.value;
}
}
Following code works as expected:
.....
UserNumber b = new UserNumber(5);
short test = (short)b;
-------------------
BUT this one throws Specified cast is not valid.
.....
UserNumber b = new UserNumber(5);
TestMethod(b);
.....
private void TestMethod(object b)
<-- Param is object
{
short test;
test =
(short)b;
<-- Exception
}
=======================================
short s = 5;
TestMethod(s);
Works fine.
Why ?
I use an existing dll that I can not change (as in Example
TestMethod(object value))
I suspect this Method cast all base types (if value.GetType() ==
typeof(short) .....
but of course not UserNumber
What can i do ? I must use TestMethod
Thanks