I currently have an extension method on System.Windows.Forms.Control like this:
public static void ExampleMethod(this Control ctrl){ /* ... */ }
However, this method doesn't appear on classes derived from Control, such as PictureBox. Can I make an extension method that appears not only in Control, but for classes derived from Control, without having to do an explicit cast?
You must include the using statement for the namespace in which your extensions class is defined or the extension methods will not be in scope.
Extension methods work fine on derived types (e.g. the extension methods defined on IEnumerable<T>
in System.Linq).
An extension method will actually apply to all inheritors/implementors of the type that's being extended (in this case, Control). You might try checking your using statements to ensure the namespace that the extension method is in is being referenced where you're trying to call it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With