ThisClass by Trym Lund Flogard
Nuget / site data
Details
Info
Name: ThisClass
Exposes class and type information as constants in the ThisClass class using source generators powered by Roslyn, inspired by ThisAssembly.
Author: Trym Lund Flogard
NuGet: https://www.nuget.org/packages/ThisClass/
You can find more details at https://github.com/trympet/ThisClass
Source : https://github.com/trympet/ThisClass
Original Readme
ThisClass
Exposes class and type information as constants in the ThisClass class using source generators powered by Roslyn, inspired by ThisAssembly.
ThisClass
Add the ThisClassAttribute
to generate type information for a class.
[ThisClass]
partial class Demo
{
public Demo()
{
Logger.Info($"Hello from {ThisClass.FullName}"); // SampleApp.Demo
}
}
NLog.Extensions.ThisClass
Create class loggers without using reflection.
using SomeNamespace;
namespace SampleApp.NLog
{
...
namespace AnotherNamespace
{
using SomeOtherNamespace;
[ClassLoggerLazy]
partial class Demo2<T> : SomeInterface<T> where T : SomeOtherInterface
{
public static void SayHello()
{
Logger.Info("Hello");
}
[ClassLogger]
internal partial class NestedClass : SomeInterface<SomeOtherInterface>
{
}
}
}
}
Looks like this behind the scenes
// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
partial class Demo1
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.Demo1";
}
}
}
// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;
partial class Demo2<T> : global::SomeNamespace.SomeInterface<T> where T : global::SomeOtherNamespace.SomeOtherInterface
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.AnotherNamespace.Demo2";
}
private static global::NLog.Logger? __loggerLazy;
private static global::NLog.Logger Logger => __loggerLazy ??= global::NLog.LogManager.GetLogger(ThisClass.FullName);
}
}
}
// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;
partial class Demo2<T> : global::SomeNamespace.SomeInterface<T> where T : global::SomeOtherNamespace.SomeOtherInterface
{
partial class NestedClass : global::SomeNamespace.SomeInterface<global::SomeOtherNamespace.SomeOtherInterface>
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.AnotherNamespace.Demo2.NestedClass";
}
private static readonly global::NLog.Logger Logger = global::NLog.LogManager.GetLogger(ThisClass.FullName);
}
}
}
}
About
Generate full class name from class
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references ThisClass
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ThisClass" Version="1.5.11" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of ThisClass in Program.cs
using DemoClass;
Person person = new Person();
person.Name = "Andrei Ignat";
Console.WriteLine(person.Name);
Console.WriteLine(Person.ThisClass.FullName);
This is the use of ThisClass in Person.cs
namespace DemoClass;
[ThisClass]
internal partial class Person
{
public string Name { get; set; }
public string ClassName => ThisClass.FullName;
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Person_ThisClass.g.cs
- ThisClassAttribute.g.cs
// <auto-generated/>
#pragma warning disable CS0108, CS1591, CS1573, CS0465, CS0649, CS8019, CS1570, CS1584, CS1658, CS0436, CS8981
#nullable enable
namespace DemoClass;
partial class Person
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "DemoClass.Person";
}
}
// <auto-generated/>
#pragma warning disable CS1591,CS1573,CS0465,CS0649,CS8019,CS1570,CS1584,CS1658,CS0436,CS8981
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, Inherited = false, AllowMultiple = true)]
sealed partial class ThisClassAttribute : Attribute
{
}
Usefull
Download Example (.NET C# )
Share ThisClass
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisClass