Yes you got confused with the internal keyword. If you want to link the assembly (Two different) use the public. So that assembly one exposed class can be accessed by assembly 2.
Internal means: Internal to assembly. That means your class BaseClass can be acccessed with-in the assembly (Assembly1.dll) not outside of the binary dll in which it is defined. Hope this helps. Do further study on the same topic.
What the sample shows is that if you were to compile the code for the BaseClass into an assembly named Assembly1, and then compile the TestClass into a different assembly (Assembly1_a), which references Assembly1, the line of code marked with the comment
// CS0122 would, indeed, generate the compiler error CS0122 because BaseClass is not visible to code outside of Assembly1.
Note that unless you state otherwise - by applying the public keyword - all top level classes are internal by default. This ensures that developers are forced to "opt in" to making their types accessible from other assemblies. This is closely analagous to
members in classes being private by default, and is a solid example of choosing good, secure, defaults.
As to your question, if you were building the code and wanted to expose BaseClass outside of its assembly, simply change the access modifier to public.
Regards
Dave
Marked as answer by Angie xu - MSFT on Dec 28, 2012 06:32 AM
amigo 1
Member
62 Points
202 Posts
assemblies how to link them
Dec 21, 2012 05:39 AM|LINK
// Assembly1.cs // Compile with: /target:library internal class BaseClass { public static int intM = 0; } // Assembly1_a.cs // Compile with: /reference:Assembly1.dll class TestAccess { static void Main() { BaseClass myBase = new BaseClass(); // CS0122 } }This piece of code is taken from msdn and is an incorrect piece of coding . The point is to demonstrate internal keyword.
Anyway how to link these 2 assemblies ---is my question here?
Sirama
Member
301 Points
71 Posts
Re: assemblies how to link them
Dec 21, 2012 05:53 AM|LINK
Yes you got confused with the internal keyword. If you want to link the assembly (Two different) use the public. So that assembly one exposed class can be accessed by assembly 2.
Internal means: Internal to assembly. That means your class BaseClass can be acccessed with-in the assembly (Assembly1.dll) not outside of the binary dll in which it is defined. Hope this helps. Do further study on the same topic.
DMW
All-Star
15943 Points
2353 Posts
Re: assemblies how to link them
Dec 21, 2012 08:01 AM|LINK
It is not an incorrect piece of coding.
What the sample shows is that if you were to compile the code for the BaseClass into an assembly named Assembly1, and then compile the TestClass into a different assembly (Assembly1_a), which references Assembly1, the line of code marked with the comment // CS0122 would, indeed, generate the compiler error CS0122 because BaseClass is not visible to code outside of Assembly1.
Note that unless you state otherwise - by applying the public keyword - all top level classes are internal by default. This ensures that developers are forced to "opt in" to making their types accessible from other assemblies. This is closely analagous to members in classes being private by default, and is a solid example of choosing good, secure, defaults.
As to your question, if you were building the code and wanted to expose BaseClass outside of its assembly, simply change the access modifier to public.
Dave
amigo 1
Member
62 Points
202 Posts
Re: assemblies how to link them
Dec 25, 2012 06:03 AM|LINK
Thank you Dave
You have really helped me many a times