I was reading a question on making a generic property, but I'm a little confused by the last example from the first answer (I've included the relevant code below):
You have to know the type at compile time. If you don't know the type at compile time then you must be storing it in an object, in which case you can add the following property to the Foo class:
public object ConvertedValue {
get {
return Convert.ChangeType(Value, Type);
}
}
That's seems strange: it's converting the value to the specified type, but it's returning it as an object when the value was stored as an object. Doesn't the returned object still require un-boxing? If it does, then why bother with the conversion of the type?
I'm also trying to make a generic property whose type will be determined at run time:
public class Foo
{
object Value {get;set;}
Type ValType{get;set;}
Foo(object value, Type type)
{ Value = value; ValType = type; }
// I need a property that is actually
// returned as the specified value type...
public object ConvertedValue {
get {
开发者_开发百科 return Convert.ChangeType(Value, ValType);
}
}
}
Is it possible to make a generic property? Does the return property still require unboxing after it's accessed?
Note: I don't want to make Foo
generic because I want Foo
to contain values of different types and I want to put various Foo
s into a collection. In other words, I want to have a collection that holds different types of objects.
Convert.ChangeType()
decides on the type at run time. The parameter you give can be calculated at run time and can be something the compiler can't know on compilation.
This is why it must return a generic object and not a specific type.
It still converts the type - for example from int to double. The compilation time type is object
but the run time type changes. If you run GetType()
on that object you will get the actual run time type.
When you convert a value type to a reference type you get boxing and the other way around you get un-boxing - so this depends on the types you use.
精彩评论