Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trympet/ThisClass
Exposes class and type information as constants in the ThisClass class using source generators powered by Roslyn, inspired by ThisAssembly.
https://github.com/trympet/ThisClass
csharp-sourcegenerator reflection-free roslyn-generator source-generator
Last synced: 3 months ago
JSON representation
Exposes class and type information as constants in the ThisClass class using source generators powered by Roslyn, inspired by ThisAssembly.
- Host: GitHub
- URL: https://github.com/trympet/ThisClass
- Owner: trympet
- Created: 2021-07-20T13:50:49.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-14T12:00:03.000Z (10 months ago)
- Last Synced: 2024-11-01T14:51:52.150Z (3 months ago)
- Topics: csharp-sourcegenerator, reflection-free, roslyn-generator, source-generator
- Language: C#
- Homepage:
- Size: 80.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - https://github.com/trympet/ThisClass
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.
```csharp
[ThisClass]
partial class Demo
{
public Demo()
{
Logger.Info($"Hello from {ThisClass.FullName}"); // SampleApp.Demo
}
}
```## NLog.Extensions.ThisClass
Create class loggers without using reflection.
```csharp
using SomeNamespace;namespace SampleApp.NLog
{
...
namespace AnotherNamespace
{
using SomeOtherNamespace;
[ClassLoggerLazy]
partial class Demo2 : SomeInterface where T : SomeOtherInterface
{
public static void SayHello()
{
Logger.Info("Hello");
}[ClassLogger]
internal partial class NestedClass : SomeInterface
{
}
}
}
}
```Looks like this behind the scenes
```csharp
//
#nullable enable
namespace SampleApp.NLog
{
partial class Demo1
{
public static partial class ThisClass
{
///
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
///
public const string FullName = "SampleApp.NLog.Demo1";
}
}
}//
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;partial class Demo2 : global::SomeNamespace.SomeInterface where T : global::SomeOtherNamespace.SomeOtherInterface
{
public static partial class ThisClass
{
///
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
///
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);
}
}
}//
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;partial class Demo2 : global::SomeNamespace.SomeInterface where T : global::SomeOtherNamespace.SomeOtherInterface
{
partial class NestedClass : global::SomeNamespace.SomeInterface
{
public static partial class ThisClass
{
///
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
///
public const string FullName = "SampleApp.NLog.AnotherNamespace.Demo2.NestedClass";
}private static readonly global::NLog.Logger Logger = global::NLog.LogManager.GetLogger(ThisClass.FullName);
}
}
}
}
```