I am working in c# with .net 2.0 (i know its old)
Here is my setup:
struct propertyType
{
System.type type;
}
public class DataProperties
{
private Dictionary<propertyType, object> properties;
public void setProperty(propertyType key, object value)
{
if(value.getType == key.type) //make sure they are adding valid data
properties.add(key, value);
}
public T getProperty<T> (propertyType key)
{
return (T)prope开发者_JAVA技巧rties[key];
}
}
Then in my class that needs to pull properties it would look like
//this would be declared somewhere else for use by multiple classes.
//but for example sake its here
propertyType hash;
hash.type = typeof(byte[]);
byte[] hashCode = DataSet.properties.GetProperty<hash.type>(hash);
Now the last line is the one that doesnt work, but I'd like to work. I think the problem is that it doesnt like having a variable as the Type. In actual use there will be many different PropertyType objects so I want a way to get the properties out and cast to the correct type easily.
Does anyone know if it is for sure that the variable as the type is the problem. But at compile time it will know what hash.type is so its not that its an unknown value at compile time.
No, it is an unknown value at compile-time, as far as the compiler is concerned. Just because you've set it in the previous line doesn't mean the compiler really knows the value.
You simply can't use variables to specify type arguments in C# generics, other than with reflection. A type argument has to be the name of a type or another type parameter - it can't be an expression which evaluates to a System.Type
.
the problem is that it doesnt like having a variable as the Type
That's right. There's no way to do this that keeps compile-time safety. It's possible to use reflection to call the getProperty
method with a generic type that you only know at run time, but generics don't gain you anything in this case.
Since you're only using generics to do a type cast, why not add an overload to getProperty
that's not generic and returns object
?
As Jon Skeet and others have said, this cannot be done without reflection. So, here's some reflection code that should get you on the right track:
Type myGenericParam = typeof(byte[]);
MethodInfo method = typeof(DataProperties).GetMethod("getProperty").MakeGenericMethod(new[] { myGenericParam });
DataProperties foo = new DataProperties();
byte[] result = (byte[])method.Invoke(foo, new[] { /*parameters go here in order */ });
精彩评论