inejwstine
inejwstine

Reputation: 748

Can I restrict the visibility of C# extension methods to classes in the same assembly?

Say I have these files:

MyCode.cs

namespace MyCodeNamespace
{
  public class MyClass
  {
    //OMITTED
  }

  internal static class MyExtensions
  {
    internal static void Foo(this string str)
    {
      //OMITTED
    }
  }
}

OtherCode.cs

using MyCodeNamespace;
namespace OtherCodeNamespace
{
  //OMITTED
}

The two files are part of the same assembly. Is there any way I can make Foo accessible to MyCode.cs but not to OtherCode.cs? My question is similar to this question: C# Extension Methods only visible and accessible within one class ("private") But its accepted answer isn't really what I'm looking for. I want to make an extension method that's only visible to the code I'm working on, and according to the answer to the above question, someone could still access it by adding a "using" statement. Is there a way I can create an extension method that is only visible to my code, and nowhere else, not even by another class in the same assembly?

I ask because the syntax for calling an extension method is handy and would be useful for what I'm working on (otherwise I'd just create a private method instead), but I don't want others to see it and use it from their code in case it doesn't do what they assume it does. And thanks to Visual Studio's intellisense, my extension methods are currently showing up in the list of available methods (along with the option to add the namespace they're in).

Upvotes: 4

Views: 3402

Answers (4)

user1005462
user1005462

Reputation: 332

think about similar thing;

c# assembly, friend assembly will try InternalsVisibleTo; if your classes is closed maybe will not helpfull but you can try it;

Upvotes: 0

androbin
androbin

Reputation: 1759

I had a similar problem. I did not want the programmer to see my inner extension methods when configuring services in ASP.NET Core.

The solution for me was to add all extension methods to namespace Microsoft.Extensions.DependencyInjection that is used in Startup.cs and the user can see those methods. (As you would always do.)

If I wanted to "hide" something I added the extension method to MyNamespace.Extensions. If the user writes the correct name the helper for add using will show up but by default it won't be listed.

I know this is not a solution but might help someone.

Upvotes: 0

Steve
Steve

Reputation: 2046

Extension methods are just semantic sugar that compile to the same IL as calling the static helpers directly.

MyExtensionMethods.DoSomething(myObject);
myObject.DoSomething();

You cannot restrict it from being called, but you can remove its visibility from Intellisense.

Simply move your extension methods to a different namespace, add a using statement in your MyCode.cs and don't include that namespace in OtherCode.cs

[update]

If you really need to restrict the caller, you could try using reflection to determine and restrict, but this is a bit overkill. Best to simply use a private static helper instead of doing this.

var frame = new System.Diagnostics.StackFrame(1, false);
var method = frame.GetMethod();
var type = method.DeclaringType;
// allow|deny type

Upvotes: 2

user1228
user1228

Reputation:

There is no such thing as a namespace-limited access modifier in the .NET platform. From the docs

public : Access is not restricted.
protected : Access is limited to the containing class or types derived from the containing class.
Internal : Access is limited to the current assembly.
protected internal: Access is limited to the current assembly or types derived from the containing class.
private : Access is limited to the containing type.

That's all you have to work with. So the answer is no.

Upvotes: 5

Related Questions