bhavinb
bhavinb

Reputation: 3278

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 using reflection or runtime APIs such as System.Reflection.Assembly.GetEntryAssembly) from within Visual Studio?

The scenario requires a tool to get the assembly name that a Visual Studio project will eventually compile into.

This is like parsing the AssemblyName property of the .csproj - I am wondering if there are any APIs that can give this information reliably.

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

Upvotes: 4

Views: 4038

Answers (5)

satish
satish

Reputation: 11

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

Upvotes: 1

Grant Peters
Grant Peters

Reputation: 7825

if you are calling the tool via a post/pre-build event, this data is very easy to access.

Just go to the "project properties->Build Events" tab, then select either "edit pre-build" or "edit post-build", depending on when you want the tool to run. This should bring up an edit window with the ever helpful "Macros >>" button. Press this and you will be given a heap of macros to use and should be pretty much everything you need.

Upvotes: 3

Hemant
Hemant

Reputation: 19826

I think you will need to write some regular expression that will give you the value of "AssemblyTitle" attribute in AssemblyInfo.cs file.

Something like this:

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;
    }
}

Upvotes: -1

Rune FS
Rune FS

Reputation: 21742

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

Upvotes: 2

Flo
Flo

Reputation: 2778

After a quick run through MSDN I found this article which might be a good start for some further research:

Accessing Project Type Specific Project, Project Item, and Configuration Properties

Upvotes: 0

Related Questions