0

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.

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • Possible duplicate of [How to get the generic type at runtime?](http://stackoverflow.com/questions/3941384/how-to-get-the-generic-type-at-runtime) – Chris Pickford Mar 29 '17 at 16:28
  • no, this is not even in the near of :) but thank you for this link. or maybe i dont really get it... – Dwza Mar 29 '17 at 16:32
  • Currently your code sample isn't complete. Please see how to create a [MCVE]. – David L Mar 29 '17 at 16:41
  • @DavidL there is all code that is needed to answere my question. Doesnt even matters if it works or not. Guess you simply dont understand my intension ^^ ill add some more of my testings so you may see what i'm trying to do – Dwza Mar 29 '17 at 16:45
  • @Dwza and I don't think you understand how Stack Overflow questions are required to be written. Your code must be complete and it must reproduce the issue. I can't run the above code so it not complete. – David L Mar 29 '17 at 16:52
  • But there is now issu actually. I want to retrieve the passed generic datatype.... the methos runs like expacted what i posted... but to prevent from creating a reg file that creates some basic entrys i want to add them in the method above... and there for i need the passed generic type – Dwza Mar 29 '17 at 16:54

1 Answers1

0

You can use the typeof operator.

public T GetRegData<T>(string v)
{
    RegistryKey key = registryPointer();

    object oValue;

    if ( typeof(T) == typeof(string) )
    {
        oValue = key.GetValue(v, "");
    }
    else if (typeof(T) == typeof(int))
    {
        oValue = key.GetValue(v, 0);
    }
    else if (typeof(T) == typeof(bool))
    {
        oValue = key.GetValue(v, false);
    }
    else
    {
        oValue = key.GetValue(v, null);
    }
    ...
}
Matthew Jaspers
  • 1,546
  • 1
  • 10
  • 13