Given the list of types:
public class TypeLists
{
public static List<Type> types
{
get
{
return new List<Type>() { typeof(ValueBool), typeof(ValueInt), typeof(ValueDouble), typeof(ValueString) };
}
}
}
I would like to pass this or other lists in order to use them generically in a method which would cast an accompanying object parameter in the following manner:
public static SettingValueUnit UsingAType<T>(object value)
{
var a = (T)value;
}
I attempt to implement its use:
foreach (Type t in TypeLists.types)
{
SettingValueUnit ValueUnit = UsingAType<t>(D.SettingValue.value);
}
But I get the following error
Error 1 't' is inaccessible due to its protection level
I am constrained to .Net 3.5 compact framework. I would appreciate any help.
I'm surprised at the nature of the error method you're getting, but not the fact that you're getting an error in the first place.
You can't specify a generic argument as an expression - the aim is for it to be a compile-time specification of types. If you really want to do this, you'll need to use reflection:
// The ... is because we don't know the name of the type containing the method
Method method = typeof(...).GetMethod("UsingAType").MakeGenericMethod(t);
method.Invoke(...);
See more on this question at Stackoverflow