Generic method argument <T> detected as a variable

I am currently writing code for runtime post-processing management.
I would like to make a generic method to be more effective but I get 2 error messages:

  • CS0118: 'procType' is a variable but used like a type (at out parameter)
  • CS0119: 'Fog' is a type, which is not valid in the given context (when calling the method)
void SetPostProc<T>(T procType, string IOHID, bool defval) where T : VolumeComponent
{
    byte value = Convert.ToByte(IOHandler.GetVal(IOHID, defval));

    volumeProfile.TryGet(out procType EFX);
    {
            

    };
} 
SetPostProc(Fog, "", false);

What am I doing wrong?

Thanks for your help in advance!


First of all, if Fog is really a type, and not a variable, then you're not using the proper way of calling the generic function. If the generic type is not obvious from the first parameter, then you have to explicitly specify it like this:

Fog myFogObject = ...;
SetPostProc<Fog>(myFogObject , "", false);

However, if the type of myFogObject is known at compile time, which it seems like it is in your case, you don't have to specify the generic type because the compiler will figure it out automatically:

Fog myFogObject = ...;
SetPostProc(myFogObject , "", false);

This should solve your second error (CS0119).

The second problem is that procType is a variable referring to an object of type T, and not a type. You have to call the TryGet function by passing in the generic type parameter T like this:

volumeProfile.TryGet<T>(out T EFX);

Depending on what you're trying to do with this code, I think you don't even need the T procType parameter, just having the T generic parameter should be enough:

void SetPostProc<T>(string IOHID, bool defval) where T : VolumeComponent
{
    byte value = Convert.ToByte(IOHandler.GetVal(IOHID, defval));
    volumeProfile.TryGet(out T EFX);
    {
        // ...
    };
}

EDIT: If you still want to get the result of TryGet outside the SetPostProc function, you'll need to declare the first parameter of your function as an out parameter:

void SetPostProc<T>(out T procObj, string IOHID, bool defval) where T : VolumeComponent
{
    byte value = Convert.ToByte(IOHandler.GetVal(IOHID, defval));
    volumeProfile.TryGet(out procObj);
    {
        // ...
    };
}