Skip to content

Commit

Permalink
Xml serializer now supports IEnumerable<Something> properties on mess…
Browse files Browse the repository at this point in the history
…ages.
  • Loading branch information
udidahan committed Jul 22, 2011
1 parent 2251779 commit 0099153
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IM2 : IM1
MyDic Lookup { get; set; }
Dictionary<string, List<Foo>> Foos { get; set; }
byte[] Data { get; set; }
IEnumerable<string> SomeStrings { get; set; }
}

public class Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void TestInterfaces()
o.Foos = new Dictionary<string, List<Foo>>();
o.Foos["foo1"] = new List<Foo>(new[] { new Foo { Name="1", Title = "1"}, new Foo { Name = "2", Title = "2"}});
o.Data = new byte[] { 1, 2, 3, 4, 5, 4, 3, 2, 1};
o.SomeStrings = new List<string> { "a", "b", "c" };

o.Parent = mapper.CreateInstance<IM1>();
o.Parent.Name = "udi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public void InitType(Type t)
InitType(arr[0]);
}

if (t.IsGenericType && t.IsInterface) //handle IEnumerable<Something>
{
var g = t.GetGenericArguments();
var e = typeof(IEnumerable<>).MakeGenericType(g);

if (e.IsAssignableFrom(t))
typesToCreateForEnumerables[t] = typeof(List<>).MakeGenericType(g);
}

return;
}

Expand Down Expand Up @@ -551,6 +560,9 @@ private object GetPropertyValue(Type type, XmlNode n)
if (isArray)
typeToCreate = typesToCreateForArrays[type];

if (typesToCreateForEnumerables.ContainsKey(type)) //handle IEnumerable<Something>
typeToCreate = typesToCreateForEnumerables[type];

if (typeof(IList).IsAssignableFrom(typeToCreate))
{
var list = Activator.CreateInstance(typeToCreate) as IList;
Expand Down Expand Up @@ -860,6 +872,7 @@ private static List<string> GetBaseTypes(IMessage[] messages, IMessageMapper map
private static readonly Dictionary<Type, IEnumerable<PropertyInfo>> typeToProperties = new Dictionary<Type, IEnumerable<PropertyInfo>>();
private static readonly Dictionary<Type, IEnumerable<FieldInfo>> typeToFields = new Dictionary<Type, IEnumerable<FieldInfo>>();
private static readonly Dictionary<Type, Type> typesToCreateForArrays = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, Type> typesToCreateForEnumerables = new Dictionary<Type, Type>();
private static readonly List<Type> typesBeingInitialized = new List<Type>();

private static readonly Dictionary<PropertyInfo, LateBoundProperty> propertyInfoToLateBoundProperty = new Dictionary<PropertyInfo, LateBoundProperty>();
Expand Down

0 comments on commit 0099153

Please sign in to comment.