Get assembly name at compile time in Visual Studio

Is there a way to find out the assembly name at design time (i.e. not use in Visual Studio?

The script requires a tool to get the assembly name, which will eventually be compiled by the Visual Studio project.

This is similar to parsing the AssemblyName.csproj property. I am wondering if there are any APIs that can reliably convey this information.

Please do not respond to runtime APIs that use reflection - there is no assembly file present at the time I need the assembly name - only assembly metadata in the csproj file.

+2


source to share


5 answers


The "API" you can use is LINQ to XML after the whole .csproj file is just xml. (and you can get the location of the .csproj file, if you need, from the solution file, which is not XML for some reason, but can be easily parsed)



+2


source


if you call the tool through the post / pre-build event, this data is very readily available.



Simply go to the Project Properties → Build Events tab, then select either edit pre-build or edit post-build, whichever you want the tool to run. This should bring up an edit window with a useful "Macros →" button. Click that button and you will be presented with a bunch of macros to use and should be pretty much everything you need.

+3


source


You can use the "TargetName" available in macros for Post-build events. It will give you the build name for your project.

+1


source


After a quick start on MSDN, I found this article that might be a good start for further research:

Access to specific project, project type and project properties

0


source


I think you will need to write a regex that will give you the value of the AssemblyTitle attribute in the AssemblyInfo.cs file.

Something like that:

public class Assembly
{
    public static string GetTitle (string fileFullName) {
        var contents = File.ReadAllText (fileFullName); //may raise exception if file doesn't exist

        //regex string is: AssemblyTitle\x20*\(\x20*"(?<Title>.*)"\x20*\)
        //loading from settings because it is annoying to type it in editor
        var reg = new Regex (Settings.Default.Expression);
        var match = reg.Match (contents);
        var titleGroup = match.Groups["Title"];
        return (match.Success && titleGroup.Success) ? titleGroup.Value : String.Empty;
    }
}

      

-1


source







All Articles