1 day ago · Tech · hide

In another episode of I thought I knew enough about C#… Today I wanted to “overwrite” the validations of a property from a parent DTO on its child DTO. Let’s say I had a Movie and Clip and needed shorter names for clips. Just take a look at the attributes on Movie and ShortClip. using System.ComponentModel.DataAnnotations; namespace TestProject1; [TestClass] public class NewKeywordTests { public abstract class Movie { [Required] [MinLength(52)] // <-- public string? Name { get; set; } } public class ShortClip : Movie { [MaxLength(20)] // <-- Different annotations public new string? Name { get; set; } // ^^^ } [TestMethod] public void NewDoesNotHideValidationAttributes() { var myClip = new ShortClip { Name = "AShortName" // ^^^^^ // It has less than 20 chars, // so it must be valid, right? }; var context = new ValidationContext(myClip); var validationResults = new List<ValidationResult>(); bool isValid = Validator.TryValidateObject( myClip, context, validationResults,…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town