samedi 12 mars 2022

Pass Type with condition to generic method

Hi I have two generic methods like below:

public class MethodA<Type>() where Type : class
{
     Debug.Log("MethodA is called");
}
public class MethodB<Type>()
{
     if(typeof(Type).IsClass)
         MethodA<Type>();
}

MethodA is a generic method that has constraint only class type are accepted, MethodB on the other hand does not have any restriction.

I want to call MethodA from MethodB is that generic Type is class type. however it will show error and tell me I cannot ensure that the Type that I put in is a class type even though I already check it one line earlier.

What should I do?





Aucun commentaire:

Enregistrer un commentaire