Type Constrained Extension Method

I have a set of user controls that are defined like this.

public class Control1: UserControl, Shop.Stock.IBlue public class Control2: UserControl, Shop.Stock.IBlue public class Control2: UserControl, Shop.Stock.IBlue 

Note that there are about 200 of them, and they are named better in a real project.

I want to write an extension method for objects based on UserControl and implement the Shop.Stock.IBlue interface

I do not want the extension method to simply consist of UserControl

Is there a way to do this without adding to a new base class?

+4
source share
1 answer

If I understand the question correctly, you want to define an extension method that applies only to UserControls that implement IBlue .

  public static void Foo<T>( this T obj ) where T : UserControl, IBlue { } 
+8
source

Source: https://habr.com/ru/post/1397014/


All Articles