Hi!
Here is my case: I have some type of value that is wrapped in another type with the corresponding implicit converters. If I pass the wrapped type to an object and then try to get the original value, I can only do this in two-step mode. If this code is simplified, the following code:
public enum MyEnum : int { First, Second } public class Test<T> { public Test(T val) { Value = val; } private T Value { get; set; } public static implicit operator T(Test<T> m) { return m.Value; } public static implicit operator Test<T>(T m) { var res = new Test<T>(m); return res; } } static void Main() { object res = new Test<MyEnum>(MyEnum.First); Console.WriteLine((MyEnum)(Test<MyEnum>)res); Console.WriteLine((MyEnum)res); }
First, "Console.WriteLine" works fine. The second is unsuccessful.
Is there a way to change this behavior and make it work without a double cast?
UPDATE 1
I have to use the object for the cast value (in a real application I need to set the ComboBox.SelectedItem property, and I donβt want to add an additional property to the ComboBox, because I have to change the user interface code everywhere).
UPDATE 2
Implicit conversions to and from System.Object are not allowed.
UPDATE 3
Updated my sample code to reflect the whole problem.
source share