❔ 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());
}
}
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());
}
}
11 Replies
Ice_trooper
Ice_trooperOP2y ago
What happens if I have an object of type IntegerTempClass under the variable someBaseClass, will Method2 get called in this case? Or how to design the code so that this Method2 is able to call itself regardless of the type given in the generic?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ice_trooper
Ice_trooperOP2y ago
Let me check that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ice_trooper
Ice_trooperOP2y ago
It doesn't work Updated code:
using System;

public class Program
{
public class TempBaseClass
{
public TempBaseClass()
{}
}

public class TempGenericClass<T> : TempBaseClass
{
public TempGenericClass() : base()
{}

public event Action<T> someEvent;

public void Raise(T raisedValue)
{
someEvent?.Invoke(raisedValue);
}

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 ClientClass(TempBaseClass baseClass)
{
someBaseClass = baseClass;
}

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)
{
Console.WriteLine(text);
}

public void Method2(object obj)
{
Console.WriteLine(obj.ToString());
}
}

public static void Main()
{
Console.WriteLine("Hello World");
IntegerTempClass intEvent = new IntegerTempClass();
ClientClass cc = new ClientClass(intEvent);

cc.Foo();
intEvent.Raise(12);
Console.WriteLine("End World");

}
}
using System;

public class Program
{
public class TempBaseClass
{
public TempBaseClass()
{}
}

public class TempGenericClass<T> : TempBaseClass
{
public TempGenericClass() : base()
{}

public event Action<T> someEvent;

public void Raise(T raisedValue)
{
someEvent?.Invoke(raisedValue);
}

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 ClientClass(TempBaseClass baseClass)
{
someBaseClass = baseClass;
}

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)
{
Console.WriteLine(text);
}

public void Method2(object obj)
{
Console.WriteLine(obj.ToString());
}
}

public static void Main()
{
Console.WriteLine("Hello World");
IntegerTempClass intEvent = new IntegerTempClass();
ClientClass cc = new ClientClass(intEvent);

cc.Foo();
intEvent.Raise(12);
Console.WriteLine("End World");

}
}
It doesn't register Method2 to event. I checked that with .NET Fiddle online so you can paste it also
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
cap5lut#0057
REPL Result: Success
class A<T> {}
class B : A<int> {}
var instance = new B();
instance is A<object>
class A<T> {}
class B : A<int> {}
var instance = new B();
instance is A<object>
Result: bool
False
False
Compile: 459.863ms | Execution: 37.590ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
cap5lut#0057
REPL Result: Success
class A<T> {}
class B : A<int> {}
var instance = new B();
instance is A<int>
class A<T> {}
class B : A<int> {}
var instance = new B();
instance is A<int>
Result: bool
True
True
Compile: 442.724ms | Execution: 50.540ms | React with ❌ to remove this embed.
cap5lut
cap5lut2y ago
basically there lies ur problem A<int> and A<object> are "different" types
Ice_trooper
Ice_trooperOP2y ago
Yeah, I know 😅 How to fix this code design? Ok, but what should I do to make this Method2 be called? regardless of the type Is there any solution for that? I wanted to be able to have ClientClass accept any type of class, but at the same time be able to call ToString on that type when calling an event. Okay, unfortunately I can't influence the TGC code or the base class. I can only design the ClientClass differently. Well it looks like I can't do what I wanted to do. Too bad, although it's not a dealbreaker. Thanks guys!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server