Looking for a way to restrict a function in C# to only accept classes of a specific base class type?
In my current implementation, I have a base class (which can also be an interface) and n-classes that extend it.
Here is what I am currently doing:
abstract class BaseClass{
public abstract void Execute();
}
class MyClass : BaseClass {
public void Execute(){
//my code
}
}
[...]
MyFunction(Type param)
{
//check if param is type of BaseClass. If not, throw exception
}
The issue with this approach is that it allows any type of class to be passed as a parameter. I want to prevent this from happening.
In TypeScript, you could achieve this using the following syntax:
myFunction(param: {new (): BaseClass}){
//my code
}
Is there a similar method or approach that I can use in C# to achieve the same level of restriction?