Make specific internal function visible to another assembly (i.e. not all internals visible)
Asked Answered
L

1

8

It's possible to specify [assembly: InternalsVisibleTo("NameOfOtherAssembly")] in AssemblyInfo.cs.

But is it possible to restrict this to specific internal functions?

Is there e.g. an attribute that's possible to apply to each function?

[InternalVisibleTo("NameOfOtherAssembly")]
internal void ShouldBeVisible()
{}

internal void ShouldNotBeVisible()
{}

If not, is there any other way to accomplish this?

Laurasia answered 2/4, 2014 at 16:18 Comment(5)
Interesting question. Why not just declare the functions you want exposed as "public"? This concept sounds like friend classes all over again.War
@LordTakkera Simply declaring them public is plan B. There are many cases where functionality could be useful but you want to restrict access. In my case it's a library used throughout the company, but this specific function is (and should only be) used by one specific version of the client.Laurasia
@DavidS. Isn't it possible for the assembly that has that version of the client to derive your base class and implement this method? In other words, why does the implementation of functionality that is only useful in a particular assembly, have to be baked into an assembly that is distributed everywhere?Dublin
@Asad Well, in my specific case it is. So that's a nice suggestion! For academic interest though, let's say that's not possible. Maybe this method later needs to call other internals (that should remain internals).Laurasia
@DavidS. You can always structure it so that the method implemented in the derived class does only as much work as can be done in the dependent assembly, then invokes a protected method with the result. The protected method does whatever can only be done in the distributed assembly. There's also other patterns you can look into, but my point is that you should try to modularize your code so that the referencing assembly works to adapt to the referenced assembly, instead of the other way around.Dublin
E
-2
The attribute is applied at the assembly level. 
This means that it can be included at the beginning of a source code file, 
or it can be included in the AssemblyInfo file in a Visual Studio project. 
You can use the attribute to specify a single friend assembly that can access 
the internal types and members of the current assembly.

You can add the attribute at the beginning of a source code file. You can't mark a single method with it, see the MSDN - Documentation.

if you want to split the visible and non visible methods of a class, you could do the following:

In one file, define your visible methods and mark the file as visible:

InternalsVisibleTo("NameOfOtherAssembly")]
public partial class Foo
{
  internal void Visible(){}
}

In the other file, define your non visible Methods:

public partial class Foo
{
  internal void IsNotVisible(){}
}
Esquiline answered 7/4, 2014 at 8:58 Comment(2)
It says that it can be applied at the beginning of a source code file but that it applies to an assembly. Have you actually tested that what you say will work does actually work (I've not tested myself but reading the docs suggests to me it doesn't).Lottie
You are right. I tested it and it doesn't. Forget my answer ;)Esquiline

© 2022 - 2024 — McMap. All rights reserved.