bradgonesurfing
bradgonesurfing

Reputation: 32202

How to declare a method parameter in c# as being of a concrete class and interface

I want to write a method that accepts a parameter that is a Frame. In pseudo c#

public void Foo(FrameworkElement and ISomeInterface p){
    ...
}

Is this possible? It would have to work with a class defined so

public class MyGrid : Grid, ISomeInterface {
}

where it is known that Grid is a subclass of FrameworkElement

Upvotes: 2

Views: 57

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67928

You can create a generic method that enforces those constraints:

public void Foo<T>(T p) 
    where T : FrameworkElement, ISomeInterface

so here you're saying that the Type of the object passed in needs to be a FrameworkElement and implement ISomeInterface.

Upvotes: 4

Related Questions