From 9d0488291cb2023d7c44f926c21581c559fe0c1f Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Wed, 25 Aug 2021 11:57:40 +0200 Subject: [PATCH] Remove reference to System.ComponentModel.Annotations, System.ComponentModel.DataAnnotations --- XmlSchemaClassGenerator/CodeUtilities.cs | 14 ++++++++++++++ XmlSchemaClassGenerator/RestrictionModel.cs | 9 ++++----- XmlSchemaClassGenerator/TypeModel.cs | 10 ++++------ .../XmlSchemaClassGenerator.csproj | 2 -- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 3589f0e5..edd33d8c 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -381,6 +381,8 @@ public static KeyValuePair ParseNamespace(string nsArg, st public static bool IsUsingNamespace(Type t, GeneratorConfiguration conf) => UsingNamespaces.Any(n => n.Namespace == t.Namespace && n.Condition(conf)); + public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration conf) => UsingNamespaces.Any(n => n.Namespace == namespaceName && n.Condition(conf)); + public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfiguration conf) { if (IsUsingNamespace(t, conf)) @@ -400,6 +402,18 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati return new CodeTypeReference(t, conf.CodeTypeReferenceOptions); } + public static CodeTypeReference CreateTypeReference(string namespaceName, string typeName, GeneratorConfiguration conf) + { + if (IsUsingNamespace(namespaceName, conf)) + { + var typeRef = new CodeTypeReference(typeName, conf.CodeTypeReferenceOptions); + + return typeRef; + } + else + return new CodeTypeReference($"{namespaceName}.{typeName}", conf.CodeTypeReferenceOptions); + } + /// /// See https://github.com/mganss/XmlSchemaClassGenerator/issues/245 /// and https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute#remarks diff --git a/XmlSchemaClassGenerator/RestrictionModel.cs b/XmlSchemaClassGenerator/RestrictionModel.cs index 000e9bf9..79c57be5 100644 --- a/XmlSchemaClassGenerator/RestrictionModel.cs +++ b/XmlSchemaClassGenerator/RestrictionModel.cs @@ -1,7 +1,6 @@ using System; using System.CodeDom; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -89,7 +88,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode public override CodeAttributeDeclaration GetAttribute() { - var a = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(StringLengthAttribute), Configuration), + var a = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "StringLengthAttribute", Configuration), new CodeAttributeArgument(Max > 0 ? (CodeExpression)new CodePrimitiveExpression(Max) : new CodeSnippetExpression("int.MaxValue"))); if (Min > 0) { a.Arguments.Add(new CodeAttributeArgument("MinimumLength", new CodePrimitiveExpression(Min))); } @@ -120,7 +119,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode public override CodeAttributeDeclaration GetAttribute() { - return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(MaxLengthAttribute), Configuration), + return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "MaxLengthAttribute", Configuration), new CodeAttributeArgument(new CodePrimitiveExpression(Value))); } } @@ -148,7 +147,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode public override CodeAttributeDeclaration GetAttribute() { - return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(MinLengthAttribute), Configuration), + return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "MinLengthAttribute", Configuration), new CodeAttributeArgument(new CodePrimitiveExpression(Value))); } } @@ -220,7 +219,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode public override CodeAttributeDeclaration GetAttribute() { - return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(RegularExpressionAttribute), Configuration), + return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RegularExpressionAttribute", Configuration), new CodeAttributeArgument(new CodePrimitiveExpression(Value))); } } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 50162593..e355decf 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -4,8 +4,6 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -861,7 +859,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi } var ignoreAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(XmlIgnoreAttribute), Configuration)); - var notMappedAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(NotMappedAttribute), Configuration)); + var notMappedAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations.Schema", "NotMappedAttribute", Configuration)); backingField.CustomAttributes.Add(ignoreAttribute); if (requiresBackingField) @@ -949,7 +947,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi if (!IsNullable && Configuration.DataAnnotationMode != DataAnnotationMode.None) { - var requiredAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(RequiredAttribute), Configuration)); + var requiredAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RequiredAttribute", Configuration)); member.CustomAttributes.Add(requiredAttribute); } @@ -1151,7 +1149,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi if (IsKey) { - var keyAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(KeyAttribute), Configuration)); + var keyAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "KeyAttribute", Configuration)); member.CustomAttributes.Add(keyAttribute); } @@ -1529,7 +1527,7 @@ public IEnumerable GetRestrictionAttributes() if (minInclusive != null && maxInclusive != null) { var rangeAttribute = new CodeAttributeDeclaration( - CodeUtilities.CreateTypeReference(typeof(RangeAttribute), Configuration), + CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RangeAttribute", Configuration), new CodeAttributeArgument(new CodeTypeOfExpression(minInclusive.Type)), new CodeAttributeArgument(new CodePrimitiveExpression(minInclusive.Value)), new CodeAttributeArgument(new CodePrimitiveExpression(maxInclusive.Value))); diff --git a/XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj b/XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj index 51e3cd59..efec7b89 100644 --- a/XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj +++ b/XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj @@ -34,7 +34,6 @@ - @@ -49,7 +48,6 @@ -