diff --git a/src/ZeroFormatter/Formatters/Formatter.cs b/src/ZeroFormatter/Formatters/Formatter.cs index def65ad..97457c5 100644 --- a/src/ZeroFormatter/Formatters/Formatter.cs +++ b/src/ZeroFormatter/Formatters/Formatter.cs @@ -702,6 +702,11 @@ internal static object GetBuiltinFormatter(); } + + else if (HasZeroFormattableAttribute(ti)) + { + formatter = CreateDynamicFormatter(ti); + } } else if (ti.GetCustomAttributes(typeof(UnionAttribute), false).FirstOrDefault() != null) @@ -719,16 +724,9 @@ internal static object GetBuiltinFormatter(); - } - else - { - formatter = DynamicFormatter.Create(); - } + formatter = CreateDynamicFormatter(ti); } #endif @@ -736,8 +734,28 @@ internal static object GetBuiltinFormatter(TypeInfo ti) + where TTypeResolver : ITypeResolver, new() + { + if (ti.IsValueType) + { + return DynamicStructFormatter.Create(); + } + else + { + return DynamicFormatter.Create(); + } + } + + internal static void ResolveDynamicUnion(Type unionType, DynamicUnionResolver resolver) { foreach (var item in unionResolver) diff --git a/tests/ZeroFormatter.Tests/GenericTest.cs b/tests/ZeroFormatter.Tests/GenericTest.cs new file mode 100644 index 0000000..b755440 --- /dev/null +++ b/tests/ZeroFormatter.Tests/GenericTest.cs @@ -0,0 +1,87 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ZeroFormatter.Formatters; + +namespace ZeroFormatter.Tests +{ + [TestClass] + public class GenericTest + { + [ZeroFormattable] + public struct MyGenericStruct + { + [Index(0)] + public T x; + [Index(1)] + public int y; + [Index(2)] + public T z; + + public MyGenericStruct(T x, int y, T z) + { + this.x = x; + this.y = y; + this.z = z; + } + } + + [TestMethod] + public void GenericStruct() + { + var ofDouble = new MyGenericStruct(100.4f, -3, 200.5f); + + var ofDouble2 = ZeroFormatterSerializer.Convert(ofDouble); + + ofDouble2.x.Is(100.4f); + ofDouble2.y.Is(-3); + ofDouble2.z.Is(200.5f); + + var ofString = new MyGenericStruct("", int.MaxValue, "abc"); + + var ofString2 = ZeroFormatterSerializer.Convert(ofString); + + ofString2.x.Is(""); + ofString2.y.Is(int.MaxValue); + ofString2.z.Is("abc"); + } + + [ZeroFormattable] + public struct MyGenericClass + { + [Index(0)] + public T x; + [Index(1)] + public int y; + + public MyGenericClass(T x, int y) + { + this.x = x; + this.y = y; + } + } + + [TestMethod] + public void GenericClass() + { + var ofString = new MyGenericClass("xyz", int.MinValue); + + var ofString2 = ZeroFormatterSerializer.Convert(ofString); + + ofString2.x.Is("xyz"); + ofString2.y.Is(int.MinValue); + + var ofMGS = + new MyGenericClass>( + new MyGenericStruct(0, 2, 3), + int.MinValue); + + var ofMGS2 = ZeroFormatterSerializer.Convert(ofMGS); + + ofMGS2.x.x.Is(0d); + ofMGS2.x.y.Is(2); + ofMGS2.x.z.Is(3d); + ofMGS2.y.Is(int.MinValue); + } + + } +} diff --git a/tests/ZeroFormatter.Tests/ZeroFormatter.Tests.csproj b/tests/ZeroFormatter.Tests/ZeroFormatter.Tests.csproj index 1e5d935..27591b7 100644 --- a/tests/ZeroFormatter.Tests/ZeroFormatter.Tests.csproj +++ b/tests/ZeroFormatter.Tests/ZeroFormatter.Tests.csproj @@ -77,6 +77,7 @@ +