AssemblyVersionInfo by Black White Yoshi
NuGet / site data
Details
Info
Name: AssemblyVersionInfo
AssemblyVersionInfo is a very simple source generator that generates constant strings of your assembly name and version. The intended usage is for the System.CodeDom.Compiler.GeneratedCodeAttribute.
Author: Black White Yoshi
NuGet: https://www.nuget.org/packages/AssemblyVersionInfo/
You can find more details at https://github.com/BlackWhiteYoshi/AssemblyVersionInfo
Source: https://github.com/BlackWhiteYoshi/AssemblyVersionInfo
Original Readme
AssemblyVersionInfo
AssemblyVersionInfo is a very simple source generator that generates constant strings of your assembly name and version.
The intended usage is for the GeneratedCodeAttribute.
All it does is generating a the static class Assembly in the namespace AssemblyVersionInfo:
// <auto-generated/>
namespace AssemblyVersionInfo;
internal static class Assembly {
public const string NAME = "{{compilation.name}}";
public const string VERSION_MAJOR = "{{compilation.version.Major}}";
public const string VERSION_MINOR = "{{compilation.version.Minor}}";
public const string VERSION_BUILD = "{{compilation.version.Build}}";
public const string VERSION_REVISION = "{{compilation.version.Revision}}";
public const string VERSION = "{{compilation.version}}";
public const string VERSION_MAJOR_MINOR = "{{compilation.version.Major}}.{{compilation.version.Minor}}";
public const string VERSION_MAJOR_MINOR_BUILD = "{{compilation.version.Major}}.{{compilation.version.Minor}}.{{compilation.version.Build}}";
}
## Get Started
- Add PackageReference to your .csproj file.
<ItemGroup>
<PackageReference Include="AssemblyVersionInfo" Version="{latest version}" PrivateAssets="all" />
</ItemGroup>
- Access the strings in the Assembly class.
using AssemblyVersionInfo;
const string example = $"AssemblyName={Assembly.NAME}, AssemblyVersion={Assembly.VERSION}";
Console.WriteLine(example);
About
Generating assembly version and other info from csproj to csharp
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
This is the CSharp Project that references AssemblyVersionInfo
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AssemblyVersionInfo" Version="1.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
This is the use of AssemblyVersionInfo in Program.cs
// Access project information anywhere in your code
Console.WriteLine($"Project Name: {AssemblyVersionInfo.Assembly.NAME}");
Console.WriteLine($"Project Version: {AssemblyVersionInfo.Assembly.VERSION}");
;
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Assembly.g.cs
- BuilderAttribute.g.cs
- NullObjectAttribute.g.cs
- ShadowWriter.TheProject.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
namespace AssemblyVersionInfo;
/// <summary>
/// This class provides constant strings holding information about the Assembly name and version.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("AssemblyVersionInfo", "1.0.2")]
internal static class Assembly {
/// <summary>
/// The simple name of the assembly.
/// </summary>
public const string NAME = "DemoAttr";
/// <summary>
/// The major component of the version number, thats usually the first number.
/// </summary>
public const string VERSION_MAJOR = "1";
/// <summary>
/// The minor component of the version number, thats usually the second number.
/// </summary>
public const string VERSION_MINOR = "0";
/// <summary>
/// The build component of the version number, thats usually the third number.
/// </summary>
public const string VERSION_BUILD = "0";
/// <summary>
/// The revision component of the version number, thats usually the fourth number.
/// </summary>
public const string VERSION_REVISION = "0";
/// <summary>
/// <para>The full version number:</para>
/// <para>{Major}.{Minor}.{Build}.{Revision}</para>
/// </summary>
public const string VERSION = "1.0.0.0";
/// <summary>
/// <para>Version number with only major and minor:</para>
/// <para>{Major}.{Minor}</para>
/// </summary>
public const string VERSION_MAJOR_MINOR = "1.0";
/// <summary>
/// <para>Version number with only major, minor and build:</para>
/// <para>{Major}.{Minor}.{Build}</para>
/// </summary>
public const string VERSION_MAJOR_MINOR_BUILD = "1.0.0";
}
// <auto-generated/>
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
namespace ShadowWriter
{
[CompilerGenerated]
[GeneratedCode("ShadowWriter", "0.9.5.0")]
[System.AttributeUsage(AttributeTargets.Class)]
internal sealed class BuilderAttribute : System.Attribute
{
}
}
// <auto-generated/>
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
namespace ShadowWriter
{
[CompilerGenerated]
[GeneratedCode("ShadowWriter", "0.9.5.0")]
[System.AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class)]
internal sealed class NullObjectAttribute : System.Attribute
{
}
[CompilerGenerated]
[GeneratedCode("ShadowWriter", "0.9.5.0")]
[System.AttributeUsage(System.AttributeTargets.Interface)]
internal sealed class ClassNameAttribute : System.Attribute
{
public string Name { get; }
public ClassNameAttribute(string name)
{
this.Name = name;
}
public ClassNameAttribute()
{
this.Name = string.Empty;
}
}
}
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
namespace ShadowWriter;
[CompilerGenerated]
[GeneratedCode("ShadowWriter", "0.9.5.0")]
internal static class TheProject
{
public static string FullPath => @"D:\gth\RSCG_Examples\v2\rscg_examples\ShadowWriterProjectInfo\src\DemoAttr\DemoAttr\DemoAttr.csproj";
public static string ProjectDirectory => @"D:\gth\RSCG_Examples\v2\rscg_examples\ShadowWriterProjectInfo\src\DemoAttr\DemoAttr";
public static string Name => @"DemoAttr";
public static string OutDir => @"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\bin\Debug\net9.0\";
public static string Version => @"1.0.0";
public static string RootNamespace => @"DemoAttr";
public static DateTimeOffset BuildTimeUtc => new DateTimeOffset(638911364201257764, TimeSpan.Zero);
}
Useful
Download Example (.NET C#)
Share AssemblyVersionInfo
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AssemblyVersionInfo