Hiding method from everyone but two assemblies.

  • Thread starter Thread starter Dan Holmes
  • Start date Start date
D

Dan Holmes

I have assembly a, b and c. Assembly b has a static method in one of
its classes that i would like classes in a and c to access but no other
assembly.

It would be similar to internal but with the ability to specify which
classes could access this method.

I know this doesn't exist but a declaration like:

[InternalTo(A.dll, C.dll)]
internal static Method()...

I guess i could check the call stack in Method for callers that are at
the top of the stack are a.dll or c.dll. Is there a better way?

dan
 
Dan,
It would be similar to internal but with the ability to specify which
classes could access this method.

There's the InternalsVisibleToAttribute, but it applies only to
assemblies. There's nothing as granular as what you're asking for.


Mattias
 
I have assembly a, b and c. Assembly b has a static method in one of
its classes that i would like classes in a and c to access but no other
assembly.

It would be similar to internal but with the ability to specify which
classes could access this method.

Why? It seems like the easiest way to do what you want is just
duplicate the code as private in the two spots that need it. I hate
duplicating code, but you've got some pretty strict requirements that,
in this case, are easily solved with a copy/paste of the one method.
 
I use the InternalsVisibleTo attribute in a few places:

[assembly:
InternalsVisibleTo("Coversant.SoapBox.Services.SomeService,PublicKey=....")]

This has worked very well so far. It's not quite what you're looking for,
but it's close.
 
Hi,

You can use CAS.

Sign all of your assemblies with the same strong-name, then Demand the
StrongNameIdentityPermissionAttribute attribute on your public, static
methods with your assembly signature's public key so they cannot be called
from other assemblies.

The PublicKey value must be a hexadecimal string. You can use the Strong
Name utility (sn.exe) to create the key with which you'll sign your assembly
(although Visual Studio can be used to perform this operation for you).
The -k switch will create the public/private key pair. If you use -k to
create a file named, "key.snk" then use that file as input to the -p switch.
The file output from the -p switch can then be used as input to the -tp
switch to produce the hexadecimal representation of the public key that you
can assign to the PublicKey property of the
StrongNameIdentityPermissionAttribute attribute. Make sure you use the full
public key and not just the token.
 
Mattias said:
Dan,


There's the InternalsVisibleToAttribute, but it applies only to
assemblies. There's nothing as granular as what you're asking for.


Mattias
That will do it. I specified the most restrictive case but really only
needed what this attribute provides.

thanks

dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top