File tree Expand file tree Collapse file tree 3 files changed +44
-3
lines changed Expand file tree Collapse file tree 3 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2828 . CreateSyntaxProvider (
2929 static ( node , _ ) => node is ClassDeclarationSyntax ,
3030 static ( context , _ ) => ( context . Node , Symbol : ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ) )
31- . Where ( static item => ! item . Symbol . IsAbstract && item . Node . IsFirstSyntaxDeclarationForSymbol ( item . Symbol ) )
31+ . Where ( static item => item . Symbol is { IsAbstract : false , IsGenericType : false } && item . Node . IsFirstSyntaxDeclarationForSymbol ( item . Symbol ) )
3232 . Select ( static ( item , _ ) => item . Symbol ) ;
3333
3434 // Get the types that inherit from ObservableValidator and gather their info
Original file line number Diff line number Diff line change @@ -606,8 +606,8 @@ where getMethod is not null
606606 from property in validatableProperties
607607 select Expression . Call ( inst0 , validateMethod , new Expression [ ]
608608 {
609- Expression . Convert ( Expression . Call ( inst0 , property . GetMethod ) , typeof ( object ) ) ,
610- Expression . Constant ( property . Name )
609+ Expression . Convert ( Expression . Call ( inst0 , property . GetMethod ) , typeof ( object ) ) ,
610+ Expression . Constant ( property . Name )
611611 } ) ) ;
612612
613613 return Expression . Lambda < Action < object > > ( body , arg0 ) . Compile ( ) ;
Original file line number Diff line number Diff line change @@ -539,6 +539,32 @@ public void Test_ObservableRecipient_AbstractTypesDoNotTriggerCodeGeneration()
539539 Assert . IsNull ( createAllPropertiesValidatorMethod ) ;
540540 }
541541
542+ // See https://github.com/CommunityToolkit/dotnet/issues/246
543+ [ TestMethod ]
544+ public void Test_ObservableValidator_WithGenericTypeParameters ( )
545+ {
546+ GenericPerson < string > model = new ( ) ;
547+
548+ model . Name = "Bob" ;
549+
550+ model . ValidateAllProperties ( ) ;
551+
552+ Assert . IsTrue ( model . HasErrors ) ;
553+
554+ ValidationResult [ ] errors = model . GetErrors ( nameof ( model . Value ) ) . ToArray ( ) ;
555+
556+ Assert . IsNotNull ( errors ) ;
557+ Assert . AreEqual ( errors . Length , 1 ) ;
558+
559+ CollectionAssert . AreEqual ( errors [ 0 ] . MemberNames . ToArray ( ) , new [ ] { nameof ( model . Value ) } ) ;
560+
561+ model . Value = "Ross" ;
562+
563+ model . ValidateAllProperties ( ) ;
564+
565+ Assert . IsFalse ( model . HasErrors ) ;
566+ }
567+
542568 public class Person : ObservableValidator
543569 {
544570 private string ? name ;
@@ -797,4 +823,19 @@ public abstract class AbstractModelWithValidatableProperty : ObservableValidator
797823 [ MinLength ( 2 ) ]
798824 public string ? Name { get ; set ; }
799825 }
826+
827+ public class GenericPerson < T > : ObservableValidator
828+ {
829+ [ Required ]
830+ [ MinLength ( 1 ) ]
831+ public string ? Name { get ; set ; }
832+
833+ [ Required ]
834+ public T ? Value { get ; set ; }
835+
836+ public new void ValidateAllProperties ( )
837+ {
838+ base . ValidateAllProperties ( ) ;
839+ }
840+ }
800841}
You can’t perform that action at this time.
0 commit comments