Skip to content

Commit b7688ef

Browse files
committed
Fixed nullability attributes generation on .NET Standard 2.0
1 parent d6348a3 commit b7688ef

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/Attributes/NullabilityAttributesGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ public void Execute(GeneratorExecutionContext context)
3434
/// </summary>
3535
private void AddSourceCodeIfTypeIsNotPresent(GeneratorExecutionContext context, string typeFullName)
3636
{
37-
if (context.Compilation.GetTypeByMetadataName(typeFullName) is not null)
37+
// Check that the target attributes are not available in the consuming project. To ensure that
38+
// this works fine both in .NET (Core) and .NET Standard implementations, we also need to check
39+
// that the target types are defined in the reference assemblies for all target runtimes. This
40+
// avoids issues on .NET Standard with Roslyn also seeing internal types from referenced assemblies.
41+
if (context.Compilation.GetTypeByMetadataName(typeFullName) is
42+
{ ContainingModule: { MetadataName: "netstandard.dll" or "System.Runtime.dll" } })
3843
{
3944
return;
4045
}

UnitTests/UnitTests.NetCore/Mvvm/Test_INotifyPropertyChangedAttribute.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,25 @@ public partial class SampleModelWithINPCAndObservableProperties
107107
[ObservableProperty]
108108
private int y;
109109
}
110+
111+
[TestCategory("Mvvm")]
112+
[TestMethod]
113+
public void Test_INotifyPropertyChanged_WithGeneratedProperties_ExternalNetStandard20Assembly()
114+
{
115+
Assert.IsTrue(typeof(INotifyPropertyChanged).IsAssignableFrom(typeof(NetStandard.SampleModelWithINPCAndObservableProperties)));
116+
Assert.IsFalse(typeof(INotifyPropertyChanging).IsAssignableFrom(typeof(NetStandard.SampleModelWithINPCAndObservableProperties)));
117+
118+
NetStandard.SampleModelWithINPCAndObservableProperties model = new();
119+
List<PropertyChangedEventArgs> eventArgs = new();
120+
121+
model.PropertyChanged += (s, e) => eventArgs.Add(e);
122+
123+
model.X = 42;
124+
model.Y = 66;
125+
126+
Assert.AreEqual(eventArgs.Count, 2);
127+
Assert.AreEqual(eventArgs[0].PropertyName, nameof(NetStandard.SampleModelWithINPCAndObservableProperties.X));
128+
Assert.AreEqual(eventArgs[1].PropertyName, nameof(NetStandard.SampleModelWithINPCAndObservableProperties.Y));
129+
}
110130
}
111131
}

UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ProjectReference Include="..\..\Microsoft.Toolkit.Mvvm\Microsoft.Toolkit.Mvvm.csproj" />
1111
<ProjectReference Include="..\..\Microsoft.Toolkit.Diagnostics\Microsoft.Toolkit.Diagnostics.csproj" />
1212
<ProjectReference Include="..\..\Microsoft.Toolkit.Mvvm.SourceGenerators\Microsoft.Toolkit.Mvvm.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
13+
<ProjectReference Include="..\UnitTests.NetStandard\UnitTests.NetStandard.csproj" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

0 commit comments

Comments
 (0)