Skip to content

Commit 038ee1a

Browse files
committed
Fixed array reference.
Update Enum GetValue to return enum rather than number value.
1 parent 8ddcd7c commit 038ee1a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/SourceGeneration.Reflection.SourceGenerator/ReflectionSourceGenerator.Emit.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ private static void EmitField(CSharpCodeBuilder builder, SourceTypeInfo type, So
174174
{
175175
if (field.IsConst)
176176
{
177-
builder.AppendLine($@"GetValue = static _ => {CSharpCodeBuilder.GetConstantLiteral(field.ConstantValue)},");
178-
}
177+
builder.Append("GetValue = static _ => ");
178+
builder.AppendLine(
179+
type.IsEnum
180+
// Use enum value
181+
? $"{type.FullGlobalName}.{field.Name},"
182+
: $"{CSharpCodeBuilder.GetConstantLiteral(field.ConstantValue)},"
183+
);
184+
}
179185
else if (field.IsStatic)
180186
{
181187
builder.AppendLine($@"GetValue = static instance => {type.FullGlobalName}.{field.Name},");
@@ -464,9 +470,9 @@ private static void AppendParametersProperty(CSharpCodeBuilder builder, string p
464470
{
465471
if (parameters.Count == 0)
466472
{
467-
builder.AppendLine($"{propertyName} = Array.Empty<global::SourceGeneration.Reflection.SourceParameterInfo>(),");
468-
}
469-
else
473+
builder.AppendLine($"{propertyName} = global::System.Array.Empty<global::SourceGeneration.Reflection.SourceParameterInfo>(),");
474+
}
475+
else
470476
{
471477
builder.AppendBlock($"{propertyName} = new global::SourceGeneration.Reflection.SourceParameterInfo[]", () =>
472478
{

src/SourceGeneration.Reflection.Test/EnumTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ public void Enum()
1414
Assert.AreEqual("A", type.DeclaredFields[0].Name);
1515
Assert.AreEqual("B", type.DeclaredFields[1].Name);
1616

17-
Assert.AreEqual(0, type.DeclaredFields[0].GetValue(null));
18-
Assert.AreEqual(1, type.DeclaredFields[1].GetValue(null));
17+
Assert.AreEqual(EnumTestObject.A, type.DeclaredFields[0].GetValue(null));
18+
Assert.AreEqual(EnumTestObject.B, type.DeclaredFields[1].GetValue(null));
1919
}
2020
}
2121

2222
[SourceReflection]
2323
public enum EnumTestObject
2424
{
25-
A,B,
25+
A = 21,
26+
B = 49,
2627
}

0 commit comments

Comments
 (0)