File tree Expand file tree Collapse file tree 3 files changed +27
-1
lines changed
Microsoft.Toolkit.Mvvm.SourceGenerators/Attributes
UnitTests/UnitTests.NetCore Expand file tree Collapse file tree 3 files changed +27
-1
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments