Skip to content

Commit

Permalink
Remove reference to System.ComponentModel.Annotations, System.Compone…
Browse files Browse the repository at this point in the history
…ntModel.DataAnnotations
  • Loading branch information
Michael Ganss committed Aug 25, 2021
1 parent 0141ec3 commit 9d04882
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
14 changes: 14 additions & 0 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ public static KeyValuePair<NamespaceKey, string> 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))
Expand All @@ -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);
}

/// <summary>
/// See https://github.com/mganss/XmlSchemaClassGenerator/issues/245
/// and https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute#remarks
Expand Down
9 changes: 4 additions & 5 deletions XmlSchemaClassGenerator/RestrictionModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))); }

Expand Down Expand Up @@ -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)));
}
}
Expand Down Expand Up @@ -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)));
}
}
Expand Down Expand Up @@ -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)));
}
}
Expand Down
10 changes: 4 additions & 6 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1529,7 +1527,7 @@ public IEnumerable<CodeAttributeDeclaration> 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)));
Expand Down
2 changes: 0 additions & 2 deletions XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -49,7 +48,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.ComponentModel.Annotations" Version="[4.4.0]" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
Expand Down

0 comments on commit 9d04882

Please sign in to comment.