How could I hide class information in Class Library

  • Thread starter Thread starter Ray Z
  • Start date Start date
R

Ray Z

hi, there
I build a Class Library, I write a class A to implement interface IA. The
problem is when I give the dll file to others, thet can get all information
about not only IA, but also A. They can even see my protected members of
class A. How could I hide these informations about class A, just give them
the interface informations.

thanks.
 
Ray,
Make class A something other than public, such as internal.

Internal means that only that assembly will be able to see the class.

internal class A : IA
{
// ...
}

Of course this does not protect your class from reflection.

Hope this helps
Jay
 
Yes, this works. But this will generate another problem. User can not new a
interface. IA a = new IA; will not work. and, IA a = new A will not work
too.
 
Ray,
Yes that would be a problem. Normally in cases like that I create a factory
class with a static factory method to create an instance of the object, the
factory method would have a return type of IA, however it would return an
instance of the A class. Or I use a Plugin type pattern to load an instance
of the class, based on entries in the configuration file.

http://www.martinfowler.com/eaaCatalog/plugin.html

Hope this helps
Jay
 
Hi Ray,

Did you resolve your problem?
I think jay's class factory solution is suitable, it is somewhat similiar
to the COM implementation.

If you still have any question, please feel free to let met know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: How could I hide class information in Class Library
| Date: Mon, 13 Oct 2003 17:29:49 -0400
| Lines: 39
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191097
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks again. I get it and forget my last post which really show my stupid
| and lazy.
|
message
| | > Ray,
| > Make class A something other than public, such as internal.
| >
| > Internal means that only that assembly will be able to see the class.
| >
| > internal class A : IA
| > {
| > // ...
| > }
| >
| > Of course this does not protect your class from reflection.
| >
| > Hope this helps
| > Jay
| >
| > | > > hi, there
| > > I build a Class Library, I write a class A to implement interface IA.
| The
| > > problem is when I give the dll file to others, thet can get all
| > information
| > > about not only IA, but also A. They can even see my protected members
of
| > > class A. How could I hide these informations about class A, just give
| them
| > > the interface informations.
| > >
| > > thanks.
| > >
| > >
| >
| >
|
|
|
 
Thanks. Mmm, I solve part of the problem. I put internal keywork in front of
the members and functions I do not want user to see. like Jay told me. by
this way, user can not see the members easily. but, still, they can get
information when they debug the program which use this dll. Is this called
reflection? can I avoid it by using factory?
 
Hi Ray,

I think you can not avoid reflection.
You can use ILasm.exe to open you dll, you will see all the member of the
assembly, so the reflection will also can retrieve it.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: How could I hide class information in Class Library
| Date: Tue, 14 Oct 2003 10:21:39 -0400
| Lines: 91
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191208
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks. Mmm, I solve part of the problem. I put internal keywork in front
of
| the members and functions I do not want user to see. like Jay told me. by
| this way, user can not see the members easily. but, still, they can get
| information when they debug the program which use this dll. Is this called
| reflection? can I avoid it by using factory?
|
| | >
| > Hi Ray,
| >
| > Did you resolve your problem?
| > I think jay's class factory solution is suitable, it is somewhat
similiar
| > to the COM implementation.
| >
| > If you still have any question, please feel free to let met know.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: How could I hide class information in Class Library
| > | Date: Mon, 13 Oct 2003 17:29:49 -0400
| > | Lines: 39
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:191097
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Thanks again. I get it and forget my last post which really show my
| stupid
| > | and lazy.
| > |
| > message
| > | | > | > Ray,
| > | > Make class A something other than public, such as internal.
| > | >
| > | > Internal means that only that assembly will be able to see the
class.
| > | >
| > | > internal class A : IA
| > | > {
| > | > // ...
| > | > }
| > | >
| > | > Of course this does not protect your class from reflection.
| > | >
| > | > Hope this helps
| > | > Jay
| > | >
| > | > | > | > > hi, there
| > | > > I build a Class Library, I write a class A to implement interface
| IA.
| > | The
| > | > > problem is when I give the dll file to others, thet can get all
| > | > information
| > | > > about not only IA, but also A. They can even see my protected
| members
| > of
| > | > > class A. How could I hide these informations about class A, just
| give
| > | them
| > | > > the interface informations.
| > | > >
| > | > > thanks.
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
Thanks, Jeffrey. But, I think there should (or must) has a way to hide
implementation when you want to sell component, right?

"Jeffrey Tan[MSFT]" said:
Hi Ray,

I think you can not avoid reflection.
You can use ILasm.exe to open you dll, you will see all the member of the
assembly, so the reflection will also can retrieve it.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: How could I hide class information in Class Library
| Date: Tue, 14 Oct 2003 10:21:39 -0400
| Lines: 91
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191208
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks. Mmm, I solve part of the problem. I put internal keywork in front
of
| the members and functions I do not want user to see. like Jay told me. by
| this way, user can not see the members easily. but, still, they can get
| information when they debug the program which use this dll. Is this called
| reflection? can I avoid it by using factory?
|
| | >
| > Hi Ray,
| >
| > Did you resolve your problem?
| > I think jay's class factory solution is suitable, it is somewhat
similiar
| > to the COM implementation.
| >
| > If you still have any question, please feel free to let met know.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: How could I hide class information in Class Library
| > | Date: Mon, 13 Oct 2003 17:29:49 -0400
| > | Lines: 39
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:191097
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Thanks again. I get it and forget my last post which really show my
| stupid
| > | and lazy.
| > |
| > message
| > | | > | > Ray,
| > | > Make class A something other than public, such as internal.
| > | >
| > | > Internal means that only that assembly will be able to see the
class.
| > | >
| > | > internal class A : IA
| > | > {
| > | > // ...
| > | > }
| > | >
| > | > Of course this does not protect your class from reflection.
| > | >
| > | > Hope this helps
| > | > Jay
| > | >
| > | > | > | > > hi, there
| > | > > I build a Class Library, I write a class A to implement interface
| IA.
| > | The
| > | > > problem is when I give the dll file to others, thet can get all
| > | > information
| > | > > about not only IA, but also A. They can even see my protected
| members
| > of
| > | > > class A. How could I hide these informations about class A, just
| give
| > | them
| > | > > the interface informations.
| > | > >
| > | > > thanks.
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
Ray,
'internal' allows you to 'logically' hide implementation, in that other
assemblies cannot get to that implementation by simply referring to your
assembly, using your object in the C# source & compiling. Via Reflection
they can get to the object.

What it sounds like you are actually asking about is 'physically' hiding the
implementation, which is called Obfuscation. Obfuscation modifies your
assembly so that its really really hard to get the gest of the
implementation out. Note there are ways both instead of & in addition to
Obfuscation that you can use to protect your assembly.

VS.NET 2003 includes Dotfuscator Community Edition.

I haven't done a lot with Obfuscation, if you search the newsgroups for
Obfuscation you should hit a number of links for more info.

Hope this helps
Jay

Ray Z said:
Thanks, Jeffrey. But, I think there should (or must) has a way to hide
implementation when you want to sell component, right?
<<snip>>
 
Back
Top