This question has 2 parts, but first here is some example code:
public abstract class Animal
{
public static string Sound_Made()
{
return "Unknown"
}
}
public class Dog()
{
public static string Sound_Made()
{
return "Woof"
}
}
public class Cat()
{
public static string Sound_Made()
{
return "Meow"
}
}
Notice that the Sound_made is static, as it does not depend on the instance.
I tried to make a generic function of the following form:
public string Sound<T>()
{
if (typeof(T).isSubclassOf(Animal))
{
return T.Sound_Made() // This doesn't work since T is generic
}
else
{
return "This is not an animal."
}
}
I also tried using: public string Sound<T>() where T : Animal
but that doesn't allow me to add functionality for other classes, which I removed just to simplify my problem.
How would it be possible for me to call these functions in the generic function after verifying that they extend class Animal within said generic function?
Thank you!