public class someclass<T> { public void somemethod(object obj) { T o = (T)obj; } }
public void main() { someclass<byte> s = new someclass<byte>(); s.somemethod(1); // passing an int (the compiler should be passing a byte but it's not) }
public class someclass<T> { public void somemethod(object obj) { T o = (T) Convert.ChangeType(obj, typeof(T)); } }
... public void somemethod(object obj) { T o = default(T); if(typeof(T).Equals(obj.GetType()) == false ) { o = (T) Convert.ChangeType(obj, typeof(T)); } else o = (T)obj; }
someclass<byte> s = new someclass<byte>(); s.somemethod(1);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)