Chapter144:AssemblyInfo.csExamples

No Comments

Section144.1: Globaland local AssemblyInfo

HavingaglobalallowsforbetterDRYness,youneedonlyputvaluesthataredifferentintoAssemblyInfo.csfor projects that have variance. This use assumes your product has more than one visual studio project.

GlobalAssemblyInfo.cs

usingSystem.Reflection;

usingSystem.Runtime.InteropServices;

//usingStackoverflowdomainasamadeupexample

//Itiscommon,andmostlygood,touseoneGlobalAssemblyInfo.csthatisadded

//asalinktomanyprojectsofthesameproduct,detailsbelow

//Changetheseattributevaluesinlocalassemblyinfotomodifytheinformation.

[assembly:AssemblyProduct(“StackoverflowQ&A”)]

[assembly:AssemblyCompany(“Stackoverflow”)]

[assembly:AssemblyCopyright(“Copyright©Stackoverflow2016”)]

//ThefollowingGUIDisfortheIDofthetypelibifthisprojectisexposedtoCOM

[assembly:Guid(“4e4f2d33-aaab-48ea-a63d-1f0a8e3c935f”)] [assembly:ComVisible(false)]//notgoingtoexpose 😉

//Versioninformationforanassemblyconsistsofthefollowingfourvalues:

//roughlytranslatedfromIreckonitisforSO,notethattheymostlikely

//dynamicallygeneratethisfile

//                MajorVersion      -Year6being2016

//                MinorVersion        -Themonth

//                DayNumber               -Dayofmonth

//                Revision                 -Buildnumber

//YoucanspecifyallthevaluesoryoucandefaulttheBuildandRevisionNumbers

//byusingthe‘*’asshownbelow:[assembly:AssemblyVersion(“year.month.day.*”)]

[assembly:AssemblyVersion(“2016.7.00.00”)]

[assembly:AssemblyFileVersion(“2016.7.27.3839”)]

AssemblyInfo.cs-oneforeachproject

//thenthefollowingmightbeputintoaseparateAssemblyfileperproject,e.g.

[assembly:AssemblyTitle(“Stackoveflow.Redis”)]

YoucanaddtheGlobalAssemblyInfo.cstothelocalprojectusingthefollowingprocedure:

  1. SelectAdd/ExistingItem…inthecontextmenuoftheproject
  2. SelectGlobalAssemblyInfo.cs
  3. ExpandtheAdd-Buttonbyclickingonthatlittledown-arrowontherighthand
  4. Select”AddAsLink”inthebuttonsdropdownlist

Section144.2:[AssemblyVersion]

This attributeapplies aversionto theassembly.

[assembly:AssemblyVersion(“1.0.*”)]

The*characterisusedtoauto-incrementaportionoftheversionautomaticallyeverytimeyoucompile(oftenused for the “build” number)

Section144.3:Automatedversioning

Yourcodeinsourcecontrolhasversionnumberseitherbydefault(SVNidsorGitSHA1hashes)orexplicitly(Git tags). Rather than manually updating versions in AssemblyInfo.cs you can use a build time process to write the version from your source control system into your AssemblyInfo.cs files and thus onto your assemblies.

The GitVersionTaskor SemVer.Git.FodyNuGet packages are examples of the above. To use GitVersionTask, for instance, after installing the package in your project remove the Assembly*Versionattributes from your AssemblyInfo.cs files. This puts GitVersionTask in charge of versioning your assemblies.

NotethatSemanticVersioningisincreasinglythedefactostandardsothesemethodsrecommendusingsource control tags that follow SemVer.

Section144.4:Commonfields

It’s good practice to complete your AssemblyInfo’s default fields. The information may be picked up by installers and will then appear when using Programs and Features (Windows 10) to uninstall or change a program.

Theminimumshouldbe:

  • AssemblyTitle-usuallythenamespace,i.e.MyCompany.MySolution.MyProject
  • AssemblyCompany – the legal entities full name
  • AssemblyProduct – marketing may have a view here
  • AssemblyCopyright – keep it up to date as it looks scruffy otherwise

‘AssemblyTitle’becomesthe’Filedescription’whenexaminingtheDLL’sPropertiesDetailstab.

Section144.5:[AssemblyTitle]

Thisattributeisusedtogiveanametothisparticularassembly.

[assembly:AssemblyTitle(“MyProduct”)]

Section144.6:[AssemblyProduct]

This attribute is used to describe the product that this particular assembly is for. Multiple assemblies can be components of the same product, in which case they can all share the same value for this attribute.

[assembly:AssemblyProduct(“MyProduct”)]

Section144.7:[InternalsVisibleTo]

Ifyouwanttomakeinternalclassesorfunctionsofanassemblyaccessablefromanotherassemblyyoudeclare this by InternalsVisibleToand the assembly name that is allowed to access.

InthisexamplecodeintheassemblyMyAssembly.UnitTestsisallowedtocallinternalelementsfromMyAssembly.

[assembly:InternalsVisibleTo(“MyAssembly.UnitTests”)]

Thisisespeciallyusefulforunittestingtopreventunnecessarypublicdeclarations.

Section144.8:[AssemblyConfiguration]

AssemblyConfiguration: The AssemblyConfiguration attribute must have the configuration that was used to build the assembly. Use conditional compilation to properly include different assembly configurations. Use the block similar to the example below. Add as many different configurations as you commonly use.

#if(DEBUG)

[assembly:AssemblyConfiguration(“Debug”)] #else

[assembly:AssemblyConfiguration(“Release”)]

#endif

Section144.9:[AssemblyKeyFile]

WheneverwewantourassemblytoinstallinGACthenitismusttohaveastrongname.Forstrongnaming assembly we have to create a public key. To generate the .snkfile.

Tocreateastrongnamekeyfile

  1. DeveloperscommandpromptforVS2015(withadministratorAccess)
  2. Atthecommandprompt,typecdC:\Directory_Nameandpress ENTER.
  3. Atthecommandprompt,typesn-kKeyFileName.snk,andthenpressENTER.

oncethekeyFileName.snkiscreatedatspecifieddirectorythengiverefernceinyourproject.give

AssemblyKeyFileAttributeattributethepathtosnkfiletogeneratethekeywhenwebuildourclasslibrary.

Properties->AssemblyInfo.cs

[assembly:AssemblyKeyFile(@”c:\Directory_Name\KeyFileName.snk”)]

Thi will create a strong name assembly after build. After creating your strong name assembly you can then install it in GAC

HappyCoding:)

Section144.10:ReadingAssemblyAttributes

Using.NET’srichreflectionAPIs,youcangainaccesstoanassembly’smetadata.Forexample,youcangetthis

assembly’stitleattributewiththefollowingcode

usingSystem.Linq;

usingSystem.Reflection;

Assemblyassembly=typeof(this).Assembly;

vartitleAttribute=assembly.GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault();

Console.WriteLine($”Thisassemblytitleis{titleAttribute?.Title}”);

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Leave a Comment