❔ How to force method call regardless of type?

public class TempBaseClass
{ }

public class TempGenericClass<T> : TempBaseClass
{
    public event Action<T> someEvent;

    public void Register(Action<T> action)
    {
        someEvent += action;
    }
}

public class StringTempClass : TempGenericClass<string>
{ }

public class IntegerTempClass : TempGenericClass<int>
{ }

public class ClientClass
{
    // What if this will be IntegerTempClass? Would Method2 be called or none?
    public TempBaseClass someBaseClass;
    
    public void Foo()
    {
        if (someBaseClass is StringTempClass stringTempClass)
        {
            stringTempClass.Register(Method1);
        }
        else if (someBaseClass is TempGenericClass<object> objectTempClass)
        {
            objectTempClass.Register(Method2);
        }
    }

    public void Method1(string text)
    {
        Debug.Log(text);
    }

    public void Method2(object obj)
    {
        Debug.Log(obj.ToString());
    }
}
Was this page helpful?