Get Assembly name at compile time in Visual Studio

后端 未结 5 953
灰色年华
灰色年华 2021-01-15 06:40

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 Visua

相关标签:
5条回答
  • 2021-01-15 06:54

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

    0 讨论(0)
  • 2021-01-15 06:55

    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)

    0 讨论(0)
  • 2021-01-15 07:04

    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.

    0 讨论(0)
  • 2021-01-15 07:05

    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

    0 讨论(0)
  • 2021-01-15 07:15

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题