Skip to content

Commit 3fc7d1b

Browse files
Only generate SetStaticProperty if we have writable static fields
1 parent d15d4da commit 3fc7d1b

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/ThunderDesign.Net-PCL.SourceGenerators/UnifiedPropertyGenerator.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private static void GenerateInfrastructureMembers(
338338
// Add static property helper methods if we have static properties
339339
if (hasStaticProperties)
340340
{
341-
GenerateStaticPropertyHelpers(source, classSymbol);
341+
GenerateStaticPropertyHelpers(source, classSymbol, propertyFields);
342342
}
343343
}
344344

@@ -746,8 +746,11 @@ private static string GetWidestAccessibility(string getter, string setter)
746746
return getterRank >= setterRank ? getter : setter;
747747
}
748748

749-
private static void GenerateStaticPropertyHelpers(StringBuilder source, INamedTypeSymbol classSymbol)
749+
private static void GenerateStaticPropertyHelpers(StringBuilder source, INamedTypeSymbol classSymbol, List<PropertyFieldInfo> propertyFields)
750750
{
751+
// Check if we have any static fields that are NOT readonly (need SetStaticProperty)
752+
bool hasWritableStaticFields = propertyFields.Any(p => p.FieldSymbol.IsStatic && !p.FieldSymbol.IsReadOnly);
753+
751754
// Check if GetStaticProperty method already exists
752755
var genericMethodExists = classSymbol.GetMembers()
753756
.OfType<IMethodSymbol>()
@@ -775,14 +778,17 @@ public static T GetStaticProperty<T>(
775778
}");
776779
}
777780

778-
// Check if SetStaticProperty method already exists
779-
var setMethodExists = classSymbol.GetMembers()
780-
.OfType<IMethodSymbol>()
781-
.Any(m => m.Name == "SetStaticProperty" && m.IsStatic && m.IsGenericMethod);
782-
783-
if (!setMethodExists)
781+
// Only generate SetStaticProperty if we have writable static fields
782+
if (hasWritableStaticFields)
784783
{
785-
source.AppendLine(@"
784+
// Check if SetStaticProperty method already exists
785+
var setMethodExists = classSymbol.GetMembers()
786+
.OfType<IMethodSymbol>()
787+
.Any(m => m.Name == "SetStaticProperty" && m.IsStatic && m.IsGenericMethod);
788+
789+
if (!setMethodExists)
790+
{
791+
source.AppendLine(@"
786792
public static bool SetStaticProperty<T>(
787793
ref T backingStore,
788794
T value,
@@ -810,6 +816,7 @@ public static bool SetStaticProperty<T>(
810816
System.Threading.Monitor.Exit(lockObj!);
811817
}
812818
}");
819+
}
813820
}
814821
}
815822

0 commit comments

Comments
 (0)