0

I am using CodeDOM to compile a *.cs file based on the data table retrieve form SQL server. So, the compiled code should be the entity class of the particular table. I managed to compile the class as public, internal or interface classes.

Ex.

public class SomeThing(){} 

or

internal class Something(){}

But the problem is there are no attributes to make the class a private using CodeDOM. Is there any way to get the job done?

I used this code as my template http://msdn.microsoft.com/en-us/library/ms404245(v=vs.100).aspx

Aditi
  • 1,188
  • 2
  • 16
  • 44
Irshad
  • 3,071
  • 5
  • 30
  • 51
  • What happens if you use `TypeAttributes.NestedPrivate`? Note that a private class *must* be a nested class. – Jon Skeet Mar 04 '13 at 08:41
  • Unless you want to create a nested class as Jon noted, who do you expect to be able to see this `private` class? How would it differ from `internal`? – Zdeslav Vojkovic Mar 04 '13 at 08:44
  • TypeAttributes.NestedPrivate did the job. Thanks. – Irshad Mar 04 '13 at 08:48
  • @Zdeslav internal classes can be visible to the assembly it s on. But private isn't. It's completely encapsulated. Am I right? MSDN stated "Internal types or members are accessible only within files in the same assembly" – Irshad Mar 04 '13 at 08:51
  • There is no such thing as private top level class. It would be visible only to itself, which makes no sense as no other class could use it. That's why mentioned 'unless ... nested classes'. – Zdeslav Vojkovic Mar 04 '13 at 08:54

2 Answers2

1

Top level private classes don't make much sense as they would be invisible to other types. Nested classes are allowed to be private.

Here's an excerpt from C# language spec:

3.5.2 Accessibility domains

The accessibility domain of a top-level unbound type T (§4.4.3) that is declared in a program P is defined as follows:

  • If the declared accessibility of T is public, the accessibility domain of T is the program text of P and any program that references
  • P. If the declared accessibility of T is internal, the accessibility domain of T is the program text of P.

From these definitions it follows that the accessibility domain of a top-level unbound type is always at least the program text of the program in which that type is declared. The accessibility domain for a constructed type T is the intersection of the accessibility domain of the unbound generic type T and the accessibility domains of the type arguments A1, ...,AN.

The accessibility domain of a nested member M declared in a type T within a program P is defined as follows (noting that M itself may possibly be a type):

  • If the declared accessibility of M is public, the accessibility domain of M is the accessibility domain of T.
  • If the declared accessibility of M is protected internal, let D be the union of the program text of P and the program text of any type derived from T, which is declared outside P. The accessibility domain of M is the intersection of the accessibility domain of T with D.
  • If the declared accessibility of M is protected, let D be the union of the program text of T and the program text of any type derived from T. The accessibility domain of M is the intersection of the accessibility domain of T with D.
  • If the declared accessibility of M is internal, the accessibility domain of M is the intersection of the accessibility domain of T with the program text of P.
  • If the declared accessibility of M is private, the accessibility domain of M is the program text of T.
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
0

And the TypeAttributes NotPublic?