I’ve the following method to search in Registry (don’t worry, it’s a test using generics):
private static T GetValue<T>(RegistryKey key, string name)
{
using (key)
{
return (T)key.GetValue(name);
}
}
But when using type : Nullable<bool> it fails because of an invalid cast.
(I tried on numeric keys only)
What I want is to have null if key not exists, false if 0 is stored in, or true else.
How can I achieve that?
Thanks !
because generics cannot utilize type-specific behavior, casts to or from a generic type parameter are assumed to be upcasts/downcasts.
a downcast fails here, you need a conversion.
you can try the
Convertclass, but better to make the type param match the registry type.