Beispiel #1
0
        public void Test()
        {
            var values = new[]
            {
                "Id",
                "Name",
                "DecimalProp1",
                "DecimalProp2",
                "DecimalProp3",
                "DecimalProp4",
                "DecimalProp5",
                "DecimalProp6",
                "DecimalProp7",
                "DecimalProp8",
                "DecimalProp9",
                "StringProp1",
                "StringProp2",
                "StringProp3",
                "StringProp4",
                "StringProp5",
                "StringProp6",
                "StringProp7",
                "StringProp8",
                "StringProp9",
                "Realllll",
                "ReallllllyLongggggggPropertyyyyyyyyy1",
                "ReallllllyLonggggggg1Propertyyyyyyyyy1",
                "ReallllllyLongggggggPropertyyyyyyyyy2",
                "ReallllllyLonggggggg2Propertyyyyyyyyy1",
                "Parent",
                "Children",
            };

            var tree = new RadixTree();

            for (var index = 0; index < values.Length; index++)
            {
                tree.Add(values[index], index);
            }

            var assemblyName    = new AssemblyName($"DynamicTyping{Guid.NewGuid():N}");
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

            var typeBuilder   = moduleBuilder.DefineType("Person", TypeAttributes.Public | TypeAttributes.Class);
            var methodBuilder = typeBuilder.DefineMethod("Resolve", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new[] { typeof(string) });

            RadixTreeCompiler.Build(tree, methodBuilder, (il, isRef) =>
            {
                if (isRef)
                {
                    il.Emit(OpCodes.Ldarga, 0);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
            }, (il, node) =>
            {
                il.Emit(OpCodes.Ldstr, node.Key);
                il.Emit(OpCodes.Ret);
            }, il =>
            {
                il.Emit(OpCodes.Ldstr, "None");
                il.Emit(OpCodes.Ret);
            });
            var type   = typeBuilder.CreateType();
            var method = type.GetMethod(methodBuilder.Name);

            var path = @"C:\Projects\PactTest\DynamicTyping\DynamicTyping.Tests\bin\Debug\output.dll";

            File.Delete(path);
            new AssemblyGenerator().GenerateAssembly(type.Assembly, path);

            foreach (var item in values)
            {
                _testOutputHelper.WriteLine(item + " = " + (string)method.Invoke(null, new object[] { item }));
            }

            _testOutputHelper.WriteLine("Invalid = " + (string)method.Invoke(null, new object[] { "Invalid" }));
        }
        public void Setup()
        {
            var items = new List <string>
            {
                "Id",
                "Name",
                "DecimalProp1",
                "DecimalProp2",
                "DecimalProp3",
                "DecimalProp4",
                "DecimalProp5",
                "DecimalProp6",
                "DecimalProp7",
                "DecimalProp8",
                "DecimalProp9",
                "StringProp1",
                "StringProp2",
                "StringProp3",
                "StringProp4",
                "StringProp5",
                "StringProp6",
                "StringProp7",
                "StringProp8",
                "StringProp9",
                "Parent",
                "Children",
            };

            var tree = new RadixTree();

            for (var index = 0; index < items.Count; index++)
            {
                tree.Add(items[index], index);
            }

            var assemblyName    = new AssemblyName($"DynamicTyping{Guid.NewGuid():N}");
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

            var typeBuilder   = moduleBuilder.DefineType("Person", TypeAttributes.Public | TypeAttributes.Class);
            var methodBuilder = typeBuilder.DefineMethod("Resolve", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new[] { typeof(string) });

            RadixTreeCompiler.Build(tree, methodBuilder, (il, isRef) =>
            {
                if (isRef)
                {
                    il.Emit(OpCodes.Ldarga, 0);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
            }, (il, node) =>
            {
                il.Emit(OpCodes.Ldstr, node.Key);
                il.Emit(OpCodes.Ret);
            }, il =>
            {
                il.Emit(OpCodes.Ldstr, "None");
                il.Emit(OpCodes.Ret);
            });

            var type   = typeBuilder.CreateType();
            var method = type.GetMethod(methodBuilder.Name);

            _dynamic = (Func <string, string>)Delegate.CreateDelegate(typeof(Func <string, string>), method);

            _dict = new Dictionary <string, string>
            {
                { "Id", "Id" },
                { "Name", "Name" },
                { "DecimalProp1", "DecimalProp1" },
                { "DecimalProp2", "DecimalProp2" },
                { "DecimalProp3", "DecimalProp3" },
                { "DecimalProp4", "DecimalProp4" },
                { "DecimalProp5", "DecimalProp5" },
                { "DecimalProp6", "DecimalProp6" },
                { "DecimalProp7", "DecimalProp7" },
                { "DecimalProp8", "DecimalProp8" },
                { "DecimalProp9", "DecimalProp9" },
                { "StringProp1", "StringProp1" },
                { "StringProp2", "StringProp2" },
                { "StringProp3", "StringProp3" },
                { "StringProp4", "StringProp4" },
                { "StringProp5", "StringProp5" },
                { "StringProp6", "StringProp6" },
                { "StringProp7", "StringProp7" },
                { "StringProp8", "StringProp8" },
                { "StringProp9", "StringProp9" },
                { "Parent", "Parent" },
                { "Children", "Children" },
            };
        }