-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from zachsaw/stage2
Fix nullables and add support for IEnumerable with Add method
- Loading branch information
Showing
13 changed files
with
1,122 additions
and
36 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
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
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
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.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Binaron.Serializer.Infrastructure | ||
{ | ||
internal static class EnumerableWrapperWithAdd | ||
{ | ||
public static readonly ConcurrentDictionary<Type, object> Adders = new ConcurrentDictionary<Type, object>(); | ||
} | ||
|
||
internal readonly struct EnumerableWrapperWithAdd<T> | ||
{ | ||
private readonly Action<object, T> action; | ||
private readonly object result; | ||
|
||
public bool HasAddAction => action != null; | ||
|
||
public EnumerableWrapperWithAdd(IEnumerable<T> result) : this() | ||
{ | ||
action = (Action<object, T>) EnumerableWrapperWithAdd.Adders.GetOrAdd(result.GetType(), CreateAdder); | ||
this.result = result; | ||
} | ||
|
||
private static object CreateAdder(Type type) | ||
{ | ||
var method = type.GetMethod("Add", new[] {typeof(T)}); | ||
|
||
if (method == null) | ||
return null; | ||
|
||
var dynamicMethod = new DynamicMethod(Guid.NewGuid().ToString(), null, new[] {typeof(object), typeof(T)}); | ||
|
||
var il = dynamicMethod.GetILGenerator(); | ||
il.Emit(OpCodes.Ldarg_0); | ||
il.Emit(OpCodes.Ldarg_1); | ||
il.Emit(method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, method); | ||
il.Emit(OpCodes.Ret); | ||
|
||
return dynamicMethod.CreateDelegate(typeof(Action<object, T>)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(T value) => action(result, value); | ||
} | ||
} |
Oops, something went wrong.