forked from zachsaw/Binaron.Serializer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add support of IEnumerable with Add method (i.e. implementation enough for var x = new T() { e1, e2, e3 } syntax. - refactor the method of restoring IEnumerable with Add method (zachsaw#31) - support of nullable in collections and as properties (zachsaw#34, zachsaw#35, zachsaw#36)
- Loading branch information
1 parent
35d629a
commit 65074af
Showing
8 changed files
with
1,625 additions
and
742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Binaron.Serializer.Tests | ||
{ | ||
public class CustomTestCollection<T> : IEnumerable<T> | ||
{ | ||
private List<T> List = new List<T>(); | ||
|
||
public T this[int index] => List[index]; | ||
|
||
public int Count => List.Count; | ||
|
||
public void Add(T value) => List.Add(value); | ||
|
||
|
||
public IEnumerator<T> GetEnumerator() => List.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => List.GetEnumerator(); | ||
|
||
public class TestCollectionObject | ||
{ | ||
public T A { get; set; } | ||
|
||
public TestCollectionObject() | ||
{ | ||
} | ||
|
||
public TestCollectionObject(T a) | ||
{ | ||
A = a; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,5 +49,6 @@ public int NoRead | |
set { } | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Binaron.Serializer/Infrastructure/EnumerableWrapperWithAdd.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Binaron.Serializer.Infrastructure | ||
{ | ||
internal class EnumerableWrapperWithAdd<T> | ||
{ | ||
private readonly Action<object, T> Action; | ||
private readonly object Result; | ||
public bool HasAddAction { get; private set; } = false; | ||
|
||
public EnumerableWrapperWithAdd(IEnumerable<T> result) | ||
{ | ||
Result = result; | ||
var method = result.GetType().GetMethod("Add", new Type[] { typeof(T) }); | ||
|
||
if (method == null) | ||
Action = (o, v) => { }; | ||
else | ||
{ | ||
HasAddAction = true; | ||
DynamicMethod action = new DynamicMethod( | ||
"Add", | ||
null, | ||
new Type[] { typeof(object), typeof(T) }, | ||
this.GetType().Module); | ||
|
||
var il = action.GetILGenerator(); | ||
il.Emit(OpCodes.Ldarg_0); | ||
il.Emit(OpCodes.Ldarg_1); | ||
il.Emit(method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, method); | ||
il.Emit(OpCodes.Ret); | ||
|
||
Action = (Action<object, T>)action.CreateDelegate(typeof(Action<object, T>)); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(T value) | ||
{ | ||
Action(Result, value); | ||
} | ||
} | ||
} |
Oops, something went wrong.