-Make .NET 4.0 dynamic actually work (oops)

-Added JValue dynamic value conversion
This commit is contained in:
JamesNK
2010-11-25 07:11:38 +00:00
parent 5c0431464b
commit efc2310a3d
10 changed files with 179 additions and 117 deletions
@@ -3827,63 +3827,5 @@ keyword such as type of business.""
});
}
#endif
[Test]
public void SerializingIEnumerableOfTShouldRetainGenericTypeInfo()
{
CustomEnumerable<Product> products = new CustomEnumerable<Product>();
string json = JsonConvert.SerializeObject(products, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
Assert.AreEqual(@"{
""$type"": ""Newtonsoft.Json.Tests.TestObjects.Product[], Newtonsoft.Json.Tests"",
""$values"": []
}", json);
}
public class CustomEnumerable<T> : IEnumerable<T>
{
//NOTE: a simple linked list
private readonly T value;
private readonly CustomEnumerable<T> next;
private readonly int count;
private CustomEnumerable(T value, CustomEnumerable<T> next)
{
this.value = value;
this.next = next;
count = this.next.count + 1;
}
public CustomEnumerable()
{
count = 0;
}
public CustomEnumerable<T> AddFirst(T newVal)
{
return new CustomEnumerable<T>(newVal, this);
}
public IEnumerator<T> GetEnumerator()
{
if (count == 0) // last node
yield break;
yield return value;
var nextInLine = next;
while (nextInLine != null)
{
if (nextInLine.count != 0)
yield return nextInLine.value;
nextInLine = nextInLine.next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}