I want to restrict my extension method but I couldn't figure out how to do it.
- No control can find my extension method.
- Any control can find my extension method. But I don't want it to be found by those who don't have the
IMyPropinterface.
How can I hide/restrict my extension method from those classes that don't implement the IMyProp interface?
Additional explanation:
I have multiple classes inherited by MyPropBase.
eg: MyPropButton, MyPropTextBox..
I have set it to automatically apply different contents by the MyPropBase class even though they are different classes.
In this way, my extension method can do the same for all different classes, and it works for all different classes.
public interface IMyProp<T> where T : MyPropBase
{
T MyProp { get; set; }
}
public static void DoSomething<T>(this T @this) where T : Control, IMyProp<MyPropBase>
{
//
}
private MyButton CreateMyButton()
{
MyButton btn = new();
btn.DoSomething(); //Error code: CS0311
return btn;
}
public static void DoSomething<T1,T2>(this T1 @this, T2 myProp) where T1 : Control, IMyProp<T2> where T2 : MyPropBase
{
//
}
private MyButton CreateMyButton()
{
MyButton btn = new();
btn.DoSomething(btn.MyProp);
return btn;
//Button btn = new();
//btn.DoSomething(btn.MyProp); //Error code: CS1061
//return btn;
}