Skip to content

Commit

Permalink
Fix source generator for class with parameterless private constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Feb 26, 2024
1 parent 0c73a20 commit a2b0c6e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
5 changes: 0 additions & 5 deletions BTDB.SourceGenerator.Sample/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
h(container, "Hello");
}

[GenerateFor(typeof(HttpContextAccessor))]
interface xxx
{
}

[Generate]
public class DynamicEventWrapper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Logger.g.cs
// <auto-generated/>
#pragma warning disable 612,618
using System;
using System.Runtime.CompilerServices;

[CompilerGenerated]
static file class LoggerRegistration
{
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
extern static global::Logger Constr();
[ModuleInitializer]
internal static unsafe void Register4BTDB()
{
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::Logger), (container, ctx) =>
{
return (container2, ctx2) =>
{
var res = Constr();
return res;
};
});
}
}
15 changes: 15 additions & 0 deletions BTDB.SourceGenerator.Test/IOCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ private ErrorHandler(ILogger logger)
""");
}

[Fact]
public Task VerifyIocRegistrationForParameterlessPrivateConstructor()
{
// language=cs
return VerifySourceGenerator("""
[BTDB.Generate]
public class Logger
{
private Logger()
{
}
}
""");
}

[Fact]
public Task VerifyIocRegistrationForDependencyProperties()
{
Expand Down
1 change: 1 addition & 0 deletions BTDB.SourceGenerator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (symbolConstructor.Parameters.Length == 0)
{
hasDefaultConstructor = true;
constructor ??= symbolConstructor;
}

if (symbolConstructor.Parameters.Length > (constructor?.Parameters.Length ?? 0))
Expand Down

0 comments on commit a2b0c6e

Please sign in to comment.