Skip to content

Commit

Permalink
Unsupported message for Dynamic Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
GregFinzer committed Jul 3, 2018
1 parent b520a51 commit f76b7c1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
81 changes: 50 additions & 31 deletions Compare-NET-Objects/TypeComparers/PropertyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Net;

namespace KellermanSoftware.CompareNetObjects.TypeComparers
{
Expand Down Expand Up @@ -129,17 +128,20 @@ private static PropertyEntity GetSecondObjectInfo(PropertyEntity info, List<Prop

private static List<PropertyEntity> GetCurrentProperties(CompareParms parms, object objectValue, Type objectType)
{
return HandleDynamicObject(objectValue, objectType)
return HandleDynamicObject(parms.Config, objectValue, objectType)
?? HandleInterfaceMembers(parms, objectValue, objectType)
?? HandleNormalProperties(parms, objectValue, objectType);
}

private static List<PropertyEntity> HandleNormalProperties(CompareParms parms, object objectValue, Type objectType)
{
List<PropertyEntity> currentProperties = new List<PropertyEntity>();

var properties = Cache.GetPropertyInfo(parms.Result.Config, objectType);
IEnumerable<PropertyInfo> properties = Cache.GetPropertyInfo(parms.Result.Config, objectType);
return AddPropertyInfos(objectValue, objectType, properties);
}

private static List<PropertyEntity> AddPropertyInfos(object objectValue, Type objectType, IEnumerable<PropertyInfo> properties)
{
List<PropertyEntity> currentProperties = new List<PropertyEntity>();
foreach (var property in properties)
{
PropertyEntity propertyEntity = new PropertyEntity();
Expand Down Expand Up @@ -216,38 +218,55 @@ private static List<PropertyEntity> HandleInterfaceMembers(CompareParms parms, o
return currentProperties;
}

private static List<PropertyEntity> HandleDynamicObject(object objectValue, Type objectType)
private static List<PropertyEntity> HandleDynamicObject(ComparisonConfig config, object objectValue, Type objectType)
{
List<PropertyEntity> currentProperties = null;

if (TypeHelper.IsDynamicObject(objectType))
{
currentProperties = new List<PropertyEntity>();
IDictionary<string, object> propertyValues = (IDictionary<string, object>)objectValue;

foreach (var propertyValue in propertyValues)
if (TypeHelper.IsExpandoObject(objectValue))
{
PropertyEntity propertyEntity = new PropertyEntity();
propertyEntity.IsDynamic = true;
propertyEntity.Name = propertyValue.Key;
propertyEntity.Value = propertyValue.Value;
propertyEntity.CanRead = true;
propertyEntity.CanWrite = true;
propertyEntity.DeclaringType = objectType;

if (propertyValue.Value == null)
{
propertyEntity.PropertyType = null;
propertyEntity.ReflectedType = null;
}
else
{
propertyEntity.PropertyType = propertyValue.GetType();
propertyEntity.ReflectedType = propertyEntity.PropertyType;
}
return AddExpandoPropertyValues(objectValue, objectType);
}

currentProperties.Add(propertyEntity);
string msg = @"Dynamic Objects Are Not Supported as TryGetMember requires Microsoft.CSharp which requires .NET Framework 4.5 and higher. ExpandoObjects are supported.
See https://github.com/GregFinzer/Compare-Net-Objects/issues/103";
throw new NotSupportedException(msg);
}

return null;
}



private static List<PropertyEntity> AddExpandoPropertyValues(Object objectValue, Type objectType)
{
IDictionary<string, object> expandoPropertyValues = objectValue as IDictionary<string, object>;

if (expandoPropertyValues == null)
return new List<PropertyEntity>();

List<PropertyEntity> currentProperties = new List<PropertyEntity>();
foreach (var propertyValue in expandoPropertyValues)
{
PropertyEntity propertyEntity = new PropertyEntity();
propertyEntity.IsDynamic = true;
propertyEntity.Name = propertyValue.Key;
propertyEntity.Value = propertyValue.Value;
propertyEntity.CanRead = true;
propertyEntity.CanWrite = true;
propertyEntity.DeclaringType = objectType;

if (propertyValue.Value == null)
{
propertyEntity.PropertyType = null;
propertyEntity.ReflectedType = null;
}
else
{
propertyEntity.PropertyType = propertyValue.GetType();
propertyEntity.ReflectedType = propertyEntity.PropertyType;
}

currentProperties.Add(propertyEntity);
}

return currentProperties;
Expand Down
18 changes: 18 additions & 0 deletions Compare-NET-Objects/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ public static bool IsDynamicObject(Type type)
#endif
}

/// <summary>
/// Determines whether the specified object is an expando object
/// </summary>
/// <param name="objectValue">The object value.</param>
public static bool IsExpandoObject(object objectValue)
{
if (objectValue == null)
return false;

if (IsDynamicObject(objectValue.GetType()))
{
IDictionary<string, object> expandoPropertyValues = objectValue as IDictionary<string, object>;
return expandoPropertyValues != null;
}

return false;
}

/// <summary>
/// Returns true if it is a byte array
/// </summary>
Expand Down

0 comments on commit f76b7c1

Please sign in to comment.