Last post Jul 09, 2014 05:39 AM by PatriceSc
Jul 05, 2014 12:54 PM|jasminder.33|LINK
I know what is the use of the internal access modifier and also how we can use the InternalsVisibleToAttribute attribute to make our internal classes accessible outside the assembly.
I have one confusion. How can we make only some classes of a namespace to be accessible outside the assembly i.e if I have 5 internal classes in a namespace, then how can I make 3 of them accessible outside the assembly. If i add the InternalsVisibleToAttribute
attribute on 3 of these classes, I get the following warning :
'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.
Also, it doesn't seems to work and the classes are not accessible outside the assembly.
Is that we need to add 2 different namespace's, one for the classes which we need to expose and other for classes which we do not want to ?
Jul 07, 2014 09:49 PM|Shawn - MSFT|LINK
InternalsVisibleToAttribute Class Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.
In any class of MyAssembly, you can add something like
Note here this assembly attribute has to be declared outside your own namespace. For example, the following declaration will give you compilation error.
public class A
Compiler will give you the following error
assembly is not a valid attribute location for this declaration, valid attribute location is type.
Now if you move the attribute outside your own namespace, it will start to work:
public class A
Jul 08, 2014 11:03 AM|jasminder.33|LINK
Thanks for the example Shawn.
But my question is that can we have 5 classes (3 internal + 2 public). Then, if I would like to enable access to only one of the 3 internal classes out of two, then is that possible under one namespace or we need to have multiple namespaces ?
Jul 09, 2014 05:39 AM|PatriceSc|LINK
It seems to be an assembly level attribute so it should be valid for all 3 or none (or then you would have to split this accross assemblies). I've never been a big fan of tweaking visibility this way.