Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hiding method from everyone but two assemblies.

37 views
Skip to first unread message

Dan Holmes

unread,
Feb 6, 2007, 4:11:50 PM2/6/07
to
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

Patrick Steele

unread,
Feb 6, 2007, 5:54:06 PM2/6/07
to
In article <OjFIpPjS...@TK2MSFTNGP03.phx.gbl>,
danh...@bigfoot.com says...

> 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.

--
Patrick Steele
http://weblogs.asp.net/psteele

Mattias Sjögren

unread,
Feb 6, 2007, 5:49:56 PM2/6/07
to
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

John Timney (MVP)

unread,
Feb 6, 2007, 6:01:18 PM2/6/07
to
Hasve you looked at GetEntryAssembly or GetCallingAssembly to see if it can
identify if its either of the unwanted assemblys calling your static method.

http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog


"Dan Holmes" <danh...@bigfoot.com> wrote in message
news:OjFIpPjS...@TK2MSFTNGP03.phx.gbl...

Chris Mullins [MVP]

unread,
Feb 6, 2007, 6:29:11 PM2/6/07
to
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins

"Dan Holmes" <danh...@bigfoot.com> wrote in message
news:OjFIpPjS...@TK2MSFTNGP03.phx.gbl...

Dave Sexton

unread,
Feb 6, 2007, 6:39:33 PM2/6/07
to
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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

"Dan Holmes" <danh...@bigfoot.com> wrote in message
news:OjFIpPjS...@TK2MSFTNGP03.phx.gbl...

Dan Holmes

unread,
Feb 7, 2007, 5:03:40 PM2/7/07
to
Mattias Sjögren wrote:
> 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
>
That will do it. I specified the most restrictive case but really only
needed what this attribute provides.

thanks

dan

0 new messages