Skip to content

Commit

Permalink
- exposing TypeCoercionUtility
Browse files Browse the repository at this point in the history
  • Loading branch information
mckamey committed Aug 27, 2010
1 parent 05d26f2 commit 3431872
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions src/JsonFx/Serialization/TypeCoercionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ THE SOFTWARE.
namespace JsonFx.Serialization
{
/// <summary>
/// Performs type coercion
/// Type Coercion Utility
/// </summary>
internal sealed class TypeCoercionUtility :
public sealed class TypeCoercionUtility :
IResolverCacheContainer
{
#region Constants
Expand Down Expand Up @@ -149,7 +149,7 @@ internal object InstantiateObjectDefaultCtor(Type targetType)
/// </summary>
/// <param name="objectType"></param>
/// <returns>objectType instance</returns>
public object InstantiateObject(Type targetType, object args)
internal object InstantiateObject(Type targetType, object args)
{
targetType = TypeCoercionUtility.ResolveInterfaceType(targetType);

Expand Down Expand Up @@ -191,26 +191,28 @@ public object InstantiateObject(Type targetType, object args)
}
}
}

IDictionary otherArgs = args as IDictionary;
if (otherArgs != null)
else
{
for (int i=0, length=ctorArgs.Length; i<length; i++)
IDictionary otherArgs = args as IDictionary;
if (otherArgs != null)
{
string name = factory.CtorArgs[i].Name;
Type type = factory.CtorArgs[i].ParameterType;

foreach (string key in otherArgs.Keys)
for (int i=0, length=ctorArgs.Length; i<length; i++)
{
try
string name = factory.CtorArgs[i].Name;
Type type = factory.CtorArgs[i].ParameterType;

foreach (string key in otherArgs.Keys)
{
if (StringComparer.OrdinalIgnoreCase.Equals(key, name))
try
{
ctorArgs[i] = this.CoerceType(type, otherArgs[key]);
break;
if (StringComparer.OrdinalIgnoreCase.Equals(key, name))
{
ctorArgs[i] = this.CoerceType(type, otherArgs[key]);
break;
}
}
catch { }
}
catch { }
}
}
}
Expand Down Expand Up @@ -269,7 +271,18 @@ internal void SetMemberValue(object target, Type targetType, MemberMap memberMap
#region Coercion Methods

/// <summary>
/// Coerces the object value to the Type targetType
/// Coerces the object value to Type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public T CoerceType<T>(object value)
{
return (T)this.CoerceType(typeof(T), value);
}

/// <summary>
/// Coerces the object value to Type of <paramref name="targetType"/>
/// </summary>
/// <param name="targetType"></param>
/// <param name="value"></param>
Expand Down Expand Up @@ -872,7 +885,7 @@ private static bool IsNullable(Type type)
/// <param name="value"></param>
/// <typeparam name="T">Attribute Type</typeparam>
/// <returns>true if defined</returns>
public static bool HasAttribute<T>(MemberInfo info)
internal static bool HasAttribute<T>(MemberInfo info)
where T : Attribute
{
return (info != null && Attribute.IsDefined(info, typeof(T)));
Expand All @@ -883,7 +896,7 @@ public static bool HasAttribute<T>(MemberInfo info)
/// </summary>
/// <param name="value"></param>
/// <returns>true if defined</returns>
public static bool HasAttribute(MemberInfo info, Type type)
internal static bool HasAttribute(MemberInfo info, Type type)
{
return (info != null && type != null && Attribute.IsDefined(info, type));
}
Expand All @@ -894,7 +907,7 @@ public static bool HasAttribute(MemberInfo info, Type type)
/// <param name="value"></param>
/// <typeparam name="T">Attribute Type</typeparam>
/// <returns>requested attribute or not if not defined</returns>
public static T GetAttribute<T>(MemberInfo info)
internal static T GetAttribute<T>(MemberInfo info)
where T : Attribute
{
if (info == null || !Attribute.IsDefined(info, typeof(T)))
Expand All @@ -909,7 +922,7 @@ public static T GetAttribute<T>(MemberInfo info)
/// </summary>
/// <param name="value"></param>
/// <returns>requested attribute or not if not defined</returns>
public static Attribute GetAttribute(MemberInfo info, Type type)
internal static Attribute GetAttribute(MemberInfo info, Type type)
{
if (info == null || type == null || !Attribute.IsDefined(info, type))
{
Expand Down

0 comments on commit 3431872

Please sign in to comment.