Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a wildcard-sort functionality of making the internals visible to assemblies that matches the common assembly name?

Tags:

Is there a wildcard-sort of functionality for making the internals visible to assemblies that share common assembly's major(My english-fu is failing me) name?

For example, The.Empire is the major name of all assemblies. I tried The.Empire.*, but to no avail.

Assembly A

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("The.Empire.*")]


namespace The.Empire
{
    internal static class Constants
    {
        internal readonly static string CommonId = "Luke";
    }
}

Assembly B

namespace The.Empire.Strikes
{
    public class Mate
    {
        public void AccessSomething()
        {
            Console.WriteLine("{0}", Constants.CommonId); // inaccessible
        }
    }
}

Assembly C

namespace The.Empire.Back
{
    public class Mate
    {
        public void AccessSomething()
        {
            Console.WriteLine("{0}", Constants.CommonId); // inaccessible
        }
    }
}

Is this possible? On OOP inheritance analogy, something analogous to protected, only the deriver has access to protected

It's not a loose coupling if I put the specific assembly name on InternalsVisibleTo. Other implementors of Assembly A can't be catered unless I recompile AssemblyA

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("The.Empire.Strikes")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("The.Empire.Back")]
like image 272
Hao Avatar asked Aug 12 '11 02:08

Hao


People also ask

What is InternalsVisibleTo?

The InternalsVisibleTo attribute is a well-known attribute for testing assemblies. The internal methods of an assembly become visible to the test project. This allows you to test the internal methods without using reflection, so your tests are more maintainable.

Where do you put InternalsVisibleTo?

Assembly-level attributes can appear in any code file; however, by convention they are placed in the AssemblyInfo file. This aids discoverability, as this is where people (or tools) will commonly look when investigating issues. I have been using InternalsVisibleTo for many years and placing it in the AssemblyInfo file.


1 Answers

The documentation suggests that the only ways to specify multiple assemblies is either with multiple attributes or with multiple names in a single attribute:

You can define multiple friend assemblies in two ways. They can appear as individual assembly-level attributes ... [or they] can also appear with separate InternalsVisibleToAttribute tags but a single assembly keyword.

That is:

[assembly:InternalsVisibleTo("Friend1a")]
[assembly:InternalsVisibleTo("Friend1b")]

or:

[assembly:InternalsVisibleTo("Friend2a"), 
          InternalsVisibleTo("Friend2b")]

I don't think what you want is possible, and likely intentionally so. Making your internals visible to another assembly is something that should be used sparingly, since it allows more coupling than might otherwise be desirable. Thus it should difficult to show your internals unless you absolutely meant them to be shown to a given assembly. Wildcards actively works against that.

Your best option (if you don't want to use multiple attributes) is to either a) merge the assemblies so that they can just naturally share internals or b) make certain key internal members public (or protected) and make those the integration points for the other assemblies.

like image 145
dlev Avatar answered Oct 24 '22 17:10

dlev