Skip to content

Commit

Permalink
Added EncodingResolverAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pruiz committed Apr 15, 2016
1 parent 653a0dc commit f68e734
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions HermaFx.Foundation/HermaFx.Foundation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="RuntimeExtensions\SysDate.cs" />
<Compile Include="RuntimeExtensions\TypeExtensions.cs" />
<Compile Include="Text\EncodingConverter.cs" />
<Compile Include="Text\EncodingResolverAttribute.cs" />
<Compile Include="Text\IEncodingResolver.cs" />
<Compile Include="X509Certificates\CertificateString.cs" />
<Compile Include="ServiceLocation\ServiceLocator.cs" />
Expand Down
16 changes: 12 additions & 4 deletions HermaFx.Foundation/Text/EncodingConverter.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ public EncodingConverter()

}

private Encoding GetEncoding(string name)
private IEncodingResolver GetResolver(ITypeDescriptorContext context)
{
if (_resolver != null && !AllEncodings.Any(x => x == name.ToLowerInvariant()))
var attr = context.PropertyDescriptor.Attributes.OfType<EncodingResolverAttribute>().SingleOrDefault();
return attr != null ? attr.GetResolver() : _resolver;
}

private Encoding GetEncoding(ITypeDescriptorContext context, string name)
{
var resolver = GetResolver(context);

if (resolver != null && !AllEncodings.Any(x => x == name.ToLowerInvariant()))
return _resolver.GetEncoding(name);

return Encoding.GetEncoding(name);
Expand All @@ -51,7 +59,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
Guard.IsNotNull(value, nameof(value));
Guard.Against<NotSupportedException>(!CanConvertFrom(context, value.GetType()), "Cannot convert from value: {0}", value);

return value is Encoding ? value : GetEncoding(value as string);
return value is Encoding ? value : GetEncoding(context, value as string);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Expand All @@ -67,7 +75,7 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
var name = value as string;
if (name != null)
{
return destinationType == typeof(string) ? value : GetEncoding(name);
return destinationType == typeof(string) ? value : GetEncoding(context, name);
}

return base.ConvertTo(context, culture, value, destinationType);
Expand Down
27 changes: 27 additions & 0 deletions HermaFx.Foundation/Text/EncodingResolverAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace HermaFx.Text
{
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
sealed class EncodingResolverAttribute : Attribute
{
readonly Type _resolver;

public IEncodingResolver GetResolver()
{
return (IEncodingResolver)Activator.CreateInstance(_resolver);
}

// This is a positional argument
public EncodingResolverAttribute(Type resolver)
{
Guard.IsNotNull(resolver, nameof(resolver));
Guard.Against<ArgumentOutOfRangeException>(
typeof(IEncodingResolver).IsAssignableFrom(resolver),
"Type {0} does not implement {1}", resolver.FullName, typeof(IEncodingResolver).FullName
);

_resolver = resolver;
}
}
}

0 comments on commit f68e734

Please sign in to comment.