Skip to content

Commit 7c7affe

Browse files
Ensure access modifier of partial properties is inspected and used correctly
1 parent 2cf2d5f commit 7c7affe

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/PropertyAttributeTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public string FirstName
6060
[InlineData("public")]
6161
[InlineData("private")]
6262
[InlineData("protected")]
63+
[InlineData("protected internal")]
64+
[InlineData("internal")]
6365
[Theory]
6466
public void GeneratePartialProperty(string accessModifier)
6567
{

src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/ViewModelGenerateInterfaceAttributeTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public partial class EmployeeViewModel
2626
2727
[Property] public partial string MiddleName {{ get; set; }}
2828
29+
[Property] internal partial string InternalNotForInterface {{ get; set; }}
30+
2931
public string LastName {{ get; set; }}
3032
3133
public string FullName => FirstName + "" "" + LastName;
@@ -75,6 +77,21 @@ public partial string MiddleName
7577
}}
7678
}}
7779
}}
80+
81+
private string _internalNotForInterface;
82+
83+
internal partial string InternalNotForInterface
84+
{{
85+
get => _internalNotForInterface;
86+
set
87+
{{
88+
if (_internalNotForInterface != value)
89+
{{
90+
_internalNotForInterface = value;
91+
OnPropertyChanged(""InternalNotForInterface"");
92+
}}
93+
}}
94+
}}
7895
}}
7996
8097
public interface IEmployeeViewModel : System.ComponentModel.INotifyPropertyChanged

src/MvvmGen.SourceGenerators/Inspectors/ViewModelGenerateInterfaceAttributeInspector.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ internal static class ViewModelGenerateInterfaceAttributeInspector
4747
{
4848
foreach (var propertyToGenerate in propertiesToGenerate)
4949
{
50-
properties ??= new();
51-
properties.Add(new InterfaceProperty(propertyToGenerate.PropertyName,
52-
propertyToGenerate.PropertyType, propertyToGenerate.IsReadOnly));
50+
if (propertyToGenerate.AccessModifier == "public") // partial properties can have other access modifiers than public, for example internal
51+
{
52+
properties ??= new();
53+
properties.Add(new InterfaceProperty(propertyToGenerate.PropertyName,
54+
propertyToGenerate.PropertyType, propertyToGenerate.IsReadOnly));
55+
}
5356
}
5457
}
5558

src/MvvmGen.SourceGenerators/Inspectors/ViewModelMemberInspector.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,18 @@ private static void FindPropertiesToGenerate(ISymbol symbol, List<PropertyToGene
216216
}
217217

218218
var generateBackingField = symbol.Kind == SymbolKind.Property;
219+
var accessModifier = "public";
220+
if(symbol.Kind== SymbolKind.Property)
221+
{
222+
accessModifier = propertySymbol!.DeclaredAccessibility switch
223+
{
224+
Accessibility.ProtectedOrInternal => "protected internal",
225+
_ => propertySymbol!.DeclaredAccessibility.ToString().ToLower()
226+
};
227+
}
219228

220229
propertiesToGenerate.Add(new PropertyToGenerate(propertyName, propertyType, fieldName,
221-
generateBackingField, isReadOnly: false, accessModifier: (symbol.Kind == SymbolKind.Field ? "public" : propertySymbol!.DeclaredAccessibility.ToString().ToLower()))
230+
generateBackingField, isReadOnly: false, accessModifier)
222231
{
223232
EventsToPublish = eventsToPublish,
224233
MethodsToCall = methodsToCall

0 commit comments

Comments
 (0)