Skip to content

Commit 9d04882

Browse files
author
Michael Ganss
committed
Remove reference to System.ComponentModel.Annotations, System.ComponentModel.DataAnnotations
1 parent 0141ec3 commit 9d04882

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ public static KeyValuePair<NamespaceKey, string> ParseNamespace(string nsArg, st
381381

382382
public static bool IsUsingNamespace(Type t, GeneratorConfiguration conf) => UsingNamespaces.Any(n => n.Namespace == t.Namespace && n.Condition(conf));
383383

384+
public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration conf) => UsingNamespaces.Any(n => n.Namespace == namespaceName && n.Condition(conf));
385+
384386
public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfiguration conf)
385387
{
386388
if (IsUsingNamespace(t, conf))
@@ -400,6 +402,18 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati
400402
return new CodeTypeReference(t, conf.CodeTypeReferenceOptions);
401403
}
402404

405+
public static CodeTypeReference CreateTypeReference(string namespaceName, string typeName, GeneratorConfiguration conf)
406+
{
407+
if (IsUsingNamespace(namespaceName, conf))
408+
{
409+
var typeRef = new CodeTypeReference(typeName, conf.CodeTypeReferenceOptions);
410+
411+
return typeRef;
412+
}
413+
else
414+
return new CodeTypeReference($"{namespaceName}.{typeName}", conf.CodeTypeReferenceOptions);
415+
}
416+
403417
/// <summary>
404418
/// See https://github.com/mganss/XmlSchemaClassGenerator/issues/245
405419
/// and https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute#remarks

XmlSchemaClassGenerator/RestrictionModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.CodeDom;
33
using System.Collections.Generic;
4-
using System.ComponentModel.DataAnnotations;
54
using System.Linq;
65
using System.Text;
76
using System.Threading.Tasks;
@@ -89,7 +88,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode
8988

9089
public override CodeAttributeDeclaration GetAttribute()
9190
{
92-
var a = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(StringLengthAttribute), Configuration),
91+
var a = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "StringLengthAttribute", Configuration),
9392
new CodeAttributeArgument(Max > 0 ? (CodeExpression)new CodePrimitiveExpression(Max) : new CodeSnippetExpression("int.MaxValue")));
9493
if (Min > 0) { a.Arguments.Add(new CodeAttributeArgument("MinimumLength", new CodePrimitiveExpression(Min))); }
9594

@@ -120,7 +119,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode
120119

121120
public override CodeAttributeDeclaration GetAttribute()
122121
{
123-
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(MaxLengthAttribute), Configuration),
122+
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "MaxLengthAttribute", Configuration),
124123
new CodeAttributeArgument(new CodePrimitiveExpression(Value)));
125124
}
126125
}
@@ -148,7 +147,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode
148147

149148
public override CodeAttributeDeclaration GetAttribute()
150149
{
151-
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(MinLengthAttribute), Configuration),
150+
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "MinLengthAttribute", Configuration),
152151
new CodeAttributeArgument(new CodePrimitiveExpression(Value)));
153152
}
154153
}
@@ -220,7 +219,7 @@ public override DataAnnotationMode MinimumDataAnnotationMode
220219

221220
public override CodeAttributeDeclaration GetAttribute()
222221
{
223-
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(RegularExpressionAttribute), Configuration),
222+
return new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RegularExpressionAttribute", Configuration),
224223
new CodeAttributeArgument(new CodePrimitiveExpression(Value)));
225224
}
226225
}

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Collections;
55
using System.Collections.Generic;
66
using System.ComponentModel;
7-
using System.ComponentModel.DataAnnotations;
8-
using System.ComponentModel.DataAnnotations.Schema;
97
using System.Diagnostics;
108
using System.Globalization;
119
using System.Linq;
@@ -861,7 +859,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
861859
}
862860

863861
var ignoreAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(XmlIgnoreAttribute), Configuration));
864-
var notMappedAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(NotMappedAttribute), Configuration));
862+
var notMappedAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations.Schema", "NotMappedAttribute", Configuration));
865863
backingField.CustomAttributes.Add(ignoreAttribute);
866864

867865
if (requiresBackingField)
@@ -949,7 +947,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
949947

950948
if (!IsNullable && Configuration.DataAnnotationMode != DataAnnotationMode.None)
951949
{
952-
var requiredAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(RequiredAttribute), Configuration));
950+
var requiredAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RequiredAttribute", Configuration));
953951
member.CustomAttributes.Add(requiredAttribute);
954952
}
955953

@@ -1151,7 +1149,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
11511149

11521150
if (IsKey)
11531151
{
1154-
var keyAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(KeyAttribute), Configuration));
1152+
var keyAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "KeyAttribute", Configuration));
11551153
member.CustomAttributes.Add(keyAttribute);
11561154
}
11571155

@@ -1529,7 +1527,7 @@ public IEnumerable<CodeAttributeDeclaration> GetRestrictionAttributes()
15291527
if (minInclusive != null && maxInclusive != null)
15301528
{
15311529
var rangeAttribute = new CodeAttributeDeclaration(
1532-
CodeUtilities.CreateTypeReference(typeof(RangeAttribute), Configuration),
1530+
CodeUtilities.CreateTypeReference("System.ComponentModel.DataAnnotations", "RangeAttribute", Configuration),
15331531
new CodeAttributeArgument(new CodeTypeOfExpression(minInclusive.Type)),
15341532
new CodeAttributeArgument(new CodePrimitiveExpression(minInclusive.Value)),
15351533
new CodeAttributeArgument(new CodePrimitiveExpression(maxInclusive.Value)));

XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
3636
<Reference Include="System" />
37-
<Reference Include="System.ComponentModel.DataAnnotations" />
3837
<Reference Include="System.Core" />
3938
<Reference Include="System.Xml.Linq" />
4039
<Reference Include="System.Data.DataSetExtensions" />
@@ -49,7 +48,6 @@
4948

5049
<ItemGroup>
5150
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
52-
<PackageReference Include="System.ComponentModel.Annotations" Version="[4.4.0]" />
5351
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
5452
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
5553
</ItemGroup>

0 commit comments

Comments
 (0)