Is it possible to grab the type that is used when i call a function ?
Sample
public T GetRegData<T>(string v)
{
RegistryKey key = registryPointer();
object oValue = key.GetValue(v, null);
if (oValue != null)
{
string sValue = oValue.ToString().ToLower();
}
if (oValue is T)
{
return (T)oValue;
}
else
{
try
{
return (T)Convert.ChangeType(oValue, typeof(T));
}
catch (InvalidCastException)
{
return default(T);
}
}
}
if I call my function it looks like:
string x = GetRegData<string>("test1");
bool y = GetRegData<bool>("test2");
Now I want to know inside my function what type i try to return. Not the type of my return data.
Reason:
key.GetValue(v, null);
Has NULL as parameter. But this will deny creating the entry in some cases. Eg. I try to add a string in the registry, it will simply skip it. It only adds it if I use string! So the part
key.GetValue(v, XXXXXX );
should be kind of dynamic ^^
Here some Tesing to code that may show more what I'm trying to do...
public T GetRegData<T>(string v)
{
RegistryKey key = registryPointer();
object oValue;
if ( default(T) is string )
{
oValue = key.GetValue(v, "");
}
else if (default(T) is int)
{
oValue = key.GetValue(v, 0);
}
else if (default(T) is bool)
{
oValue = key.GetValue(v, false);
}
else
{
oValue = key.GetValue(v, null);
}
....
But Defaut( T ) seems to be null anyway....
PS: Please stop voting close when the other question and its answeres doesnt solve my problem.