Such an actor didn’t actually do anything, because you are assigning back a variable with more free typing. Your best approach depends on what you want to do with the value type after casting.
Options:
- Reflection
- C # 4.0
dynamic . - A simple
switch or if .. else based on the type of the variable.
Edit: using a generic class doesn't actually solve your problem if you don't know anything about the type at compile time, but your example does not illustrate the actual use of your class, so it may be that I misinterpret your intentions. Maybe something similar to what you are looking for:
class MyClass<T> where T: struct { T obj; public T Obj { get { return obj; } set { obj = value; } } } MyClass<int> test = new MyClass<int>(); test.Obj = 1;
The actual type definition is now outside your class, as a typical type parameter. Strictly speaking, the type is still static because it is known at compile time.
source share