Important reason is unit testing if you want to write

Info icon This preview shows pages 622–624. Sign up to view the full content.

View Full Document Right Arrow Icon
important reason is unit testing: if you want to write unit tests for an implementation detail of your class, then if you don’t want to put the test code in the same project as the class under test, you’ll need to grant your test project access to the internals of the code being tested. This can be done by applying an assembly-level attribute, which normally goes in the AssemblyInfo.cs file, which you can find by expanding the Prop- erties section of your project in the Solution Explorer. Attributes are discussed in Chapter 17 , but for now, just know that you can put the code in Example 15-9 in that file. Example 15-9. Selectively making internals accessible [assembly: InternalsVisibleTo("MyProgram")] If we put this in the AssemblyInfo.cs of MyLibrary , MyProgram will now be able to use internal features such as the MyType constructor directly. But this raises an interesting problem: clearly anyone is free to write an assembly called MyProgram and by doing so, will be able to get access to the internals, so if we thought we were only opening up our code to a select few we need to think again. It’s possible to get a bit more selective than this, and for that we need to look in more detail at how assemblies are named. Naming By default, when you create a new assembly—either a program or a library—its name is based on the filename, but with the file extension stripped. This means that our two example projects in this chapter build assemblies whose filenames are MyPro- gram.exe and MyLibrary.dll . But as far as the .NET Framework is concerned, their names are MyProgram and MyLibrary , respectively, which is why Example 15-9 just specified MyProgram , and not MyProgram.exe . 598 | Chapter 15: Assemblies
Image of page 622

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Actually, that’s not the whole truth. These are the simple names , but there’s more to assembly names. We can ask the .NET Framework to show us the full name of a type’s containing assembly, using the code in Example 15-10 . Example 15-10. Getting a type’s containing assembly’s name Console.WriteLine(typeof(MyType).Assembly.FullName); Running this produces the following output: MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null As you can see, there are four parts to an assembly name. First there is the simple name, but this is followed by a version number. Assemblies always have a version number. If you don’t specify one, the compiler sets it to 0.0.0.0. But Visual Studio puts an assem- bly-level attribute in the AssemblyInfo.cs file setting it to 1.0.0.0, which is why we see that in the output. You would typically change the version each time you formally release your code. Example 15-11 shows the (unsurprising) syntax for the version attribute. Example 15-11. Setting an assembly’s version [assembly: AssemblyVersion("1.2.0.7")] The next part of the name is the culture. This is normally used only on components that contain localized resources for applications that need to support multiple lan- guages. Those kinds of assemblies usually contain no code—they hold nothing but resources. Assemblies that contain code don’t normally specify a culture, which is why
Image of page 623
Image of page 624
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern