From 3b2392c4165e1a84c4cff00078b5222cf4fc739b Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Fri, 19 Jun 2020 17:15:42 +0200 Subject: [PATCH] Add RequiredAttribute when use="required" (fixes #212) --- XmlSchemaClassGenerator/TypeModel.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 961d0c16..f8681a97 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -935,6 +935,12 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi AddDocs(member); + if (!IsNullable && Configuration.DataAnnotationMode != DataAnnotationMode.None) + { + var requiredAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(RequiredAttribute), Configuration.CodeTypeReferenceOptions)); + member.CustomAttributes.Add(requiredAttribute); + } + if (IsDeprecated) { // From .NET 3.5 XmlSerializer doesn't serialize objects with [Obsolete] >(