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)
28
28
. CreateSyntaxProvider (
29
29
static ( node , _ ) => node is ClassDeclarationSyntax ,
30
30
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 ) )
32
32
. Select ( static ( item , _ ) => item . Symbol ) ;
33
33
34
34
// 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
606
606
from property in validatableProperties
607
607
select Expression . Call ( inst0 , validateMethod , new Expression [ ]
608
608
{
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 )
611
611
} ) ) ;
612
612
613
613
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()
539
539
Assert . IsNull ( createAllPropertiesValidatorMethod ) ;
540
540
}
541
541
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
+
542
568
public class Person : ObservableValidator
543
569
{
544
570
private string ? name ;
@@ -797,4 +823,19 @@ public abstract class AbstractModelWithValidatableProperty : ObservableValidator
797
823
[ MinLength ( 2 ) ]
798
824
public string ? Name { get ; set ; }
799
825
}
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
+ }
800
841
}
You can’t perform that action at this time.
0 commit comments