Assignment you displayed cannot be done way you want.
Types of objects on either sides of assignment operator must be equal (or right-hand object must be of type inheriting left-hand object type).
So, you cant do
Type1 obj1 = new Type1();
Type type = typeof(Type1);
Type2 obj2 = (type)obj1;
You could achieve functionality you want by making your class generic, or having a generic method for getting value.
E.g.
public class TypeValue
{
public Type Type { get; private set; }
public object Value { get; set; }
public T GetValueAs<T>()
{
if (Value == null)
return default(T);
return (T)Value;
}
}
TypeValue a = new TypeValue();
a.Value = 1;
int b = a.GetValueAs<int>();
or even better
public class TypeValue<T>
{
public Type Type { get { return typeof(T); } }
public T Value { get; set; }
}
TypeValue<int> a = new TypeValue<int>();
a.Value = 1;
int b = a.Value;
Type c = a.Type;