Skip to content

Commit

Permalink
Fix compatibility with portable target.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaubry committed Feb 3, 2016
1 parent 54b4cc6 commit c3cc519
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 9 deletions.
6 changes: 3 additions & 3 deletions YamlDotNet/Helpers/GenericCollectionToNonGenericAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public GenericCollectionToNonGenericAdapter(object genericCollection, Type gener
{
this.genericCollection = genericCollection;

addMethod = genericCollectionType.GetMethod("Add");
countGetter = genericCollectionType.GetProperty("Count").GetGetMethod();
addMethod = genericCollectionType.GetPublicInstanceMethod("Add");
countGetter = genericCollectionType.GetPublicProperty("Count").GetGetMethod();

if (genericListType != null)
{
indexerSetter = genericListType.GetProperty("Item").GetSetMethod();
indexerSetter = genericListType.GetPublicProperty("Item").GetSetMethod();
}
}

Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Helpers/GenericDictionaryToNonGenericAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public GenericDictionaryToNonGenericAdapter(object genericDictionary, Type gener
this.genericDictionary = genericDictionary;
this.genericDictionaryType = genericDictionaryType;

indexerSetter = genericDictionaryType.GetProperty("Item").GetSetMethod();
indexerSetter = genericDictionaryType.GetPublicProperty("Item").GetSetMethod();
}

public void Add(object key, object value)
Expand Down Expand Up @@ -137,8 +137,8 @@ public DictionaryEnumerator(object genericDictionary, Type genericDictionaryType
var genericArguments = genericDictionaryType.GetGenericArguments();
var keyValuePairType = typeof(KeyValuePair<,>).MakeGenericType(genericArguments);

getKeyMethod = keyValuePairType.GetProperty("Key").GetGetMethod();
getValueMethod = keyValuePairType.GetProperty("Value").GetGetMethod();
getKeyMethod = keyValuePairType.GetPublicProperty("Key").GetGetMethod();
getValueMethod = keyValuePairType.GetPublicProperty("Value").GetGetMethod();

enumerator = ((IEnumerable)genericDictionary).GetEnumerator();
}
Expand Down
26 changes: 26 additions & 0 deletions YamlDotNet/Helpers/Portability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public static Type[] GetGenericArguments(this Type type)
return type.GetTypeInfo().GenericTypeArguments;
}

public static PropertyInfo GetPublicProperty(this Type type, string name)
{
return type.GetRuntimeProperty(name);
}

public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type)
{
var instancePublic = new Func<PropertyInfo, bool>(
Expand Down Expand Up @@ -240,11 +245,22 @@ public static MethodInfo GetPublicStaticMethod(this Type type, string name, para
});
}

public static MethodInfo GetPublicInstanceMethod(this Type type, string name)
{
return type.GetRuntimeMethods()
.FirstOrDefault(m => m.IsPublic && !m.IsStatic && m.Name.Equals(name));
}

public static MethodInfo GetGetMethod(this PropertyInfo property)
{
return property.GetMethod;
}

public static MethodInfo GetSetMethod(this PropertyInfo property)
{
return property.SetMethod;
}

public static IEnumerable<Type> GetInterfaces(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces;
Expand Down Expand Up @@ -338,6 +354,11 @@ public static TypeCode GetTypeCode(this Type type)
{
return Type.GetTypeCode(type);
}

public static PropertyInfo GetPublicProperty(this Type type, string name)
{
return type.GetProperty(name);
}

public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type)
{
Expand All @@ -359,6 +380,11 @@ public static MethodInfo GetPublicStaticMethod(this Type type, string name, para
return type.GetMethod(name, BindingFlags.Public | BindingFlags.Static, null, parameterTypes, null);
}

public static MethodInfo GetPublicInstanceMethod(this Type type, string name)
{
return type.GetMethod(name, BindingFlags.Public | BindingFlags.Instance);
}

private static readonly FieldInfo remoteStackTraceField = typeof(Exception)
.GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic);

Expand Down
104 changes: 103 additions & 1 deletion YamlDotNet/Serialization/NodeDeserializers/ArrayNodeDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,113 @@ bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<E
CollectionNodeDeserializer.DeserializeHelper(itemType, reader, expectedType, nestedObjectDeserializer, items, true);

var array = Array.CreateInstance(itemType, items.Count);
items.CopyTo(array);
items.CopyTo(array, 0);

value = array;
return true;
}

private sealed class ArrayList : IList
{
private object[] data;
private int count;

public ArrayList()
{
Clear();
}

public int Add(object value)
{
if (count == data.Length)
{
Array.Resize(ref data, data.Length * 2);
}
data[count] = value;
return count++;
}

public void Clear()
{
data = new object[10];
count = 0;
}

public bool Contains(object value)
{
throw new NotImplementedException();
}

public int IndexOf(object value)
{
throw new NotImplementedException();
}

public void Insert(int index, object value)
{
throw new NotImplementedException();
}

public bool IsFixedSize
{
get { return false; }
}

public bool IsReadOnly
{
get { return false; }
}

public void Remove(object value)
{
throw new NotImplementedException();
}

public void RemoveAt(int index)
{
throw new NotImplementedException();
}

public object this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}

public void CopyTo(Array array, int index)
{
Array.Copy(data, 0, array, index, count);
}

public int Count
{
get { return count; }
}

public bool IsSynchronized
{
get { return false; }
}

public object SyncRoot
{
get { return data; }
}

public IEnumerator GetEnumerator()
{
for (int i = 0; i < count; ++i)
{
yield return data[i];
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal static void DeserializeHelper(Type tItem, EventReader reader, Type expe
}
else if (canUpdate)
{
var index = result.Add(tItem.IsValueType ? Activator.CreateInstance(tItem) : null);
var index = result.Add(tItem.IsValueType() ? Activator.CreateInstance(tItem) : null);
promise.ValueAvailable += v => result[index] = TypeConverter.ChangeType(v, tItem);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected virtual void TraverseDictionary(IObjectDescriptor dictionary, IObjectG
{
visitor.VisitMappingStart(dictionary, keyType, valueType);

var isDynamic = dictionary.GetType().FullName.Equals("System.Dynamic.ExpandoObject");
var isDynamic = dictionary.Type.FullName.Equals("System.Dynamic.ExpandoObject");
foreach (DictionaryEntry entry in (IDictionary)dictionary.Value)
{
var keyString = isDynamic ? namingConvention.Apply(entry.Key.ToString()) : entry.Key.ToString();
Expand Down

0 comments on commit c3cc519

Please sign in to comment.