exporting certain classes in assembly

P

puzzlecracker

is there a way to create a library (assembly/dll) where I can only
export few class? I don't want client to use or know about other
internal classes in the assembly.

Thanks
 
A

Arne Vajhøj

puzzlecracker said:
is there a way to create a library (assembly/dll) where I can only
export few class? I don't want client to use or know about other
internal classes in the assembly.

public vs internal ?

Arne
 
A

ab2305

public vs internal ?

Arne

That could cause a problem. Say my client classes reference util
classes, that I don't want to export outside, making those internal
would break my implementation.
 
C

Chris Dunaway

Class cannot be internal, hence I would need to make constructors and
member methods to prevent internal classes from leaking to outside?

Why not? Inside an assembly, an internal class is only visible to the
other classes in that assembly. Public classes are visible outside
the assembly.

If the client references the assembly, it can only see the public
classes. The internal classes will not be visible to it.

Could you please clarify your comment about "making constructors and
member methods to prevent internal classes from leaking to outside"?

Or could you provide a simple and complete code sample that
illustrates why you can't use internal classes?

Chris
 
M

Morten Wennevik [C# MVP]

puzzlecracker said:
Class cannot be internal, hence I would need to make constructors and
member methods to prevent internal classes from leaking to outside?

thanks

Using StrongNameIdentityPermission it doesn't matter what visibility the
classes have. You can even put it on the entire assembly if you want nothing
at all available to unknown assemblies. However, you are not guaranteed that
identity permissions won't get bypassed by priviliged code, in which case you
may have no other option than to compare the current assembly's public key to
the calling assembly's public key in every constructor, using code similar to
the below

void CheckAssemblies()
{
Assembly currentAssembly = Assembly.GetAssembly(this.GetType());
Assembly callingAssembly = Assembly.GetEntryAssembly();

byte[] currentKeyData = currentAssembly.GetName().GetPublicKey();
byte[] callingKeyData = callingAssembly.GetName().GetPublicKey();

if (currentKeyData.Length != callingKeyData.Length)
throw new SecurityException("Illegal calling assembly");

for (int i = 0; i < currentKeyData.Length; i++)
{
if (currentKeyData != callingKeyData)
throw new SecurityException("Illegal calling assembly");
}
}
 
A

Arne Vajhøj

That could cause a problem. Say my client classes reference util
classes, that I don't want to export outside, making those internal
would break my implementation.

You will need to explain what you want.

I read the above as "if make class X internal to prevent
it from being used outside the assembly, then I can not
use it outside the assembly which is a problem".

You can not both eat your cake and have your cake.

If you need more granularity in access then maybe
InternalsVisibleTo can help you.

Arne
 

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

Top