0

I want to restrict my extension method but I couldn't figure out how to do it.

  1. No control can find my extension method.
  2. Any control can find my extension method. But I don't want it to be found by those who don't have the IMyProp interface.

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;
}

2 Answers 2

1

I'm not 100% sure what you want to achieve. My guess is that you would like to reuse some code and only expose the functionality to instances that have the type in the this parameter of the extension. This is what I cooked up:

namespace ConsoleApp4
{
    public static class Extensions
    {
        public static void DoSomething(this MyPropBase prop)
        {
            //
        }
    }
}

namespace ConsoleApp4
{
    public interface IMyProp<T>
        where T : MyPropBase
    {
        T MyProp { get; set; }
    }

    public class MyProp01 : MyPropBase, IMyProp<MyPropBase>
    {
        public MyPropBase MyProp { get; set; }
    }

    public class MyPropBase
    {

    }
}

Because the extension method accepts MyPropBase as this, it will only be visible to types that inherit MyPropBase.

var prop01 = new MyProp01();

prop01.DoSomething();

Is this what you are looking for?

Sign up to request clarification or add additional context in comments.

9 Comments

AFAIK extension method of the base class will not be visible to the derived class.
Runtime polymorphism should take care of that problem.
No, not true. Derived classes also see the extension method.
When you call DoSomething on MyPropBase, it will call an override of the DoSomething method in the derived class, if one exists.
@Emre If you can't inherit you can wrap it. Inject you object in the constructor. But it would need a lot of code to get that working as expected. No polymorphism can be used then. Until C# allows for multiple types to be inherited we have to cope with what we got and it's a lot.
|
1

If I understood your problem correctly, you can check the type of the object in your extension method and throw an exception if the object is not assignable from the Interface. This method will still allow all the controls to find the extension method but there will be a runtime exception for the ones that don't implement the interface.

I am building the code below based on @Paul's answer

namespace StackOverflow
{
    public static class Extensions
    {
        public static void DoSomething(this MyPropBase prop)
        {
            // if (prop is IMyProp<MyPropBase>) // Cleaner way uing is keyword
            if (typeof(IMyProp<MyPropBase>).IsAssignableFrom(prop.GetType()))
            {
                // Do work
                // This code block will only be executed from class that implement the interface IMyProp
            }
            else
            {
                throw new MethodAccessException("Method not supported");
            }
        }
    }

    public interface IMyProp<T> where T : MyPropBase
    {
        T MyProp { get; set; }
    }

    public class MyPropBase
    { }

    public class MyPropButton : MyPropBase, IMyProp<MyPropBase>
    {
        public MyPropBase MyProp { get; set; }
    }

    // Test the logic
    public class Program
    {
        public static void Main()
        {
            var test2 = new MyPropButton();
            test2.DoSomething();

            var test = new MyPropBase();
            test.DoSomething();// Exception is thrown
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.