-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support runtime property attributes via TypeDescriptor (#53)
* Support runtime property attributes via TypeDescriptor * Support nullable annotations in .NET Standard 2.0 * Refactor AttachAttribute to an extension method * Property name check should check for exact match * Bump version to 0.9.0 * Ensure validation attributes are still only considered once
- Loading branch information
Showing
7 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/MiniValidation.UnitTests/TypeDescriptorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System.ComponentModel; | ||
|
||
namespace MiniValidation.UnitTests | ||
{ | ||
internal static class TypeDescriptorExtensions | ||
{ | ||
public static void AttachAttribute(this Type type, string propertyName, Func<PropertyDescriptor, Attribute> attributeFactory) | ||
{ | ||
var ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(type).GetTypeDescriptor(type)!); | ||
|
||
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(type)) | ||
{ | ||
if (pd.Name == propertyName) | ||
{ | ||
var pdWithAttribute = TypeDescriptor.CreateProperty( | ||
type, | ||
pd, | ||
attributeFactory(pd)); | ||
|
||
ctd.OverrideProperty(pdWithAttribute); | ||
} | ||
} | ||
|
||
TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), type); | ||
} | ||
} | ||
|
||
// From https://stackoverflow.com/questions/12143650/how-to-add-property-level-attribute-to-the-typedescriptor-at-runtime | ||
internal class PropertyOverridingTypeDescriptor : CustomTypeDescriptor | ||
{ | ||
private readonly Dictionary<string, PropertyDescriptor> overridePds = new Dictionary<string, PropertyDescriptor>(); | ||
|
||
public PropertyOverridingTypeDescriptor(ICustomTypeDescriptor parent) | ||
: base(parent) | ||
{ } | ||
|
||
public void OverrideProperty(PropertyDescriptor pd) | ||
{ | ||
overridePds[pd.Name] = pd; | ||
} | ||
|
||
public override object? GetPropertyOwner(PropertyDescriptor? pd) | ||
{ | ||
var propertyOwner = base.GetPropertyOwner(pd); | ||
|
||
if (propertyOwner == null) | ||
{ | ||
return this; | ||
} | ||
|
||
return propertyOwner; | ||
} | ||
|
||
public override PropertyDescriptorCollection GetProperties() | ||
{ | ||
return GetPropertiesImpl(base.GetProperties()); | ||
} | ||
|
||
public override PropertyDescriptorCollection GetProperties(Attribute[]? attributes) | ||
{ | ||
return GetPropertiesImpl(base.GetProperties(attributes)); | ||
} | ||
|
||
private PropertyDescriptorCollection GetPropertiesImpl(PropertyDescriptorCollection pdc) | ||
{ | ||
var pdl = new List<PropertyDescriptor>(pdc.Count + 1); | ||
|
||
foreach (PropertyDescriptor pd in pdc) | ||
{ | ||
if (overridePds.ContainsKey(pd.Name)) | ||
{ | ||
pdl.Add(overridePds[pd.Name]); | ||
} | ||
else | ||
{ | ||
pdl.Add(pd); | ||
} | ||
} | ||
|
||
var ret = new PropertyDescriptorCollection(pdl.ToArray()); | ||
|
||
return ret; | ||
} | ||
} | ||
|
||
internal class TypeDescriptorOverridingProvider : TypeDescriptionProvider | ||
{ | ||
private readonly ICustomTypeDescriptor ctd; | ||
|
||
public TypeDescriptorOverridingProvider(ICustomTypeDescriptor ctd) | ||
{ | ||
this.ctd = ctd; | ||
} | ||
|
||
public override ICustomTypeDescriptor? GetTypeDescriptor(Type objectType, object? instance) | ||
{ | ||
return ctd; | ||
} | ||
} | ||
} |