Example #1
0
        private static Func <TObject, JsonData> BuildWriteMethod(ParameterExpression instance, PropertyInfo property,
                                                                 Expression valueConverter)
        {
            const string writeMethodName = nameof(IJsonConverter <object> .Write);

            var propertyValue = Expression.Property(instance, property);
            var body          = ExpressionUtils.Call(valueConverter, writeMethodName, propertyValue);

            return(Expression
                   .Lambda <Func <TObject, JsonData> >(body, instance)
                   .Compile());
        }
Example #2
0
        private static Action <TObject, TextWriter> BuildSerializeMethod(ParameterExpression instance,
                                                                         PropertyInfo property, Expression valueConverter)
        {
            const string serializeMethodName = nameof(IJsonConverter <object> .Serialize);

            var writer = Expression.Parameter(Typeof <TextWriter> .Raw, "writer");

            var propertyValue = Expression.Property(instance, property);
            var body          = ExpressionUtils.Call(valueConverter, serializeMethodName, propertyValue, writer);

            return(Expression
                   .Lambda <Action <TObject, TextWriter> >(body, instance, writer)
                   .Compile());
        }
Example #3
0
        private static Action <TObject, JsonData> BuildReadMethod(ParameterExpression instance, PropertyInfo property,
                                                                  Expression valueConverter)
        {
            const string readMethodName = nameof(IJsonConverter <object> .Read);

            var data = Expression.Parameter(Typeof <JsonData> .Raw, "data");

            var propertyValue = ExpressionUtils.Call(valueConverter, readMethodName, data);
            var body          = ExpressionUtils.SetProperty(instance, property, propertyValue);

            return(Expression
                   .Lambda <Action <TObject, JsonData> >(body, instance, data)
                   .Compile());
        }
Example #4
0
        private static DeserializeMethod <TObject> BuildDeserializeMethod(ParameterExpression instance,
                                                                          PropertyInfo property, Expression valueConverter)
        {
            const string deserializeMethodName = nameof(IJsonConverter <object> .Deserialize);

            var tokenizer = Expression.Parameter(JsonTokenizer.ByRefType, "tokenizer");

            var propertyValue = ExpressionUtils.Call(valueConverter, deserializeMethodName, tokenizer);
            var body          = ExpressionUtils.SetProperty(instance, property, propertyValue);

            return(Expression
                   .Lambda <DeserializeMethod <TObject> >(body, instance, tokenizer)
                   .Compile());
        }
Example #5
0
        public static TDelegate BuildDeserialize <TDelegate>(
            ParameterExpression instance,
            PropertyInfo property,
            Expression valueConverter) where TDelegate : Delegate
        {
            const string deserializeMethodName = nameof(IJsonConverter <object> .Deserialize);

            var source = Expression.Parameter(typeof(JsonTokenizer), "source");

            var propertyValue = ExpressionUtils.Call(valueConverter, deserializeMethodName, source);
            var body          = ExpressionUtils.SetProperty(instance, property, propertyValue);

            return(Expression
                   .Lambda <TDelegate>(body, source, instance)
                   .Compile());
        }
Example #6
0
        public static TDelegate BuildSerialize <TDelegate>(
            ParameterExpression instance,
            PropertyInfo property,
            Expression valueConverter) where TDelegate : Delegate
        {
            const string serializeMethodName = nameof(IJsonConverter <object> .Serialize);

            var output = Expression.Parameter(typeof(TextWriter), "output");

            var propertyValue = Expression.Property(instance, property);
            var body          = ExpressionUtils.Call(valueConverter, serializeMethodName, propertyValue, output);

            return(Expression
                   .Lambda <TDelegate>(body, instance, output)
                   .Compile());
        }
Example #7
0
        public static Func <TSystem, CancellationToken, Task> BuildSystemUpdateMethod <TSystem>()
        {
            var systemType = typeof(TSystem);

            string methodName;

            if (systemType == typeof(IAfterUpdateSystem))
            {
                methodName = nameof(IAfterUpdateSystem.AfterUpdate);
            }
            else if (systemType == typeof(IBeforeUpdateSystem))
            {
                methodName = nameof(IBeforeUpdateSystem.BeforeUpdate);
            }
            else if (systemType == typeof(IBootstrapSystem))
            {
                methodName = nameof(IBootstrapSystem.Bootstrap);
            }
            else if (systemType == typeof(ICleanupSystem))
            {
                methodName = nameof(ICleanupSystem.Cleanup);
            }
            else if (systemType == typeof(IInitSystem))
            {
                methodName = nameof(IInitSystem.Init);
            }
            else if (systemType == typeof(IUpdateSystem))
            {
                methodName = nameof(IUpdateSystem.Update);
            }
            else
            {
                throw Error.InvalidOperation("Isn't system type");
            }

            var system = Expression.Parameter(systemType);
            var token  = Expression.Parameter(typeof(CancellationToken));
            var call   = ExpressionUtils.Call(system, methodName, token);

            return(Expression
                   .Lambda <Func <TSystem, CancellationToken, Task> >(call, system, token)
                   .Compile());
        }
Example #8
0
        public static TDelegate BuildWrite <TDelegate>(
            ParameterExpression instance,
            PropertyInfo property,
            Expression valueConverter) where TDelegate : Delegate
        {
            const string addMethodMane   = nameof(JsonObject.Add);
            const string writeMethodName = nameof(IJsonConverter <object> .Write);

            var output = Expression.Parameter(typeof(JsonObject), "output");

            var propertyName  = Expression.Constant(property.Name);
            var propertyValue = Expression.Property(instance, property);
            var propertyData  = ExpressionUtils.Call(valueConverter, writeMethodName, propertyValue);

            var body = ExpressionUtils.Call(output, addMethodMane, propertyName, propertyData);

            return(Expression
                   .Lambda <TDelegate>(body, instance, output)
                   .Compile());
        }
Example #9
0
        public static TDelegate BuildRead <TDelegate>(
            ParameterExpression instance,
            PropertyInfo property,
            Expression valueConverter) where TDelegate : Delegate
        {
            const string tryGetMethodName = nameof(JsonObject.TryGet);
            const string readMethodName   = nameof(IJsonConverter <object> .Read);

            var source = Expression.Parameter(typeof(JsonObject), "source");

            var propertyName   = Expression.Constant(property.Name);
            var propertyData   = Expression.Variable(Typeof <JsonData> .Raw, "propertyData");
            var propertyExists = ExpressionUtils.Call(source, tryGetMethodName, propertyName, propertyData);

            var propertyValue = ExpressionUtils.Call(valueConverter, readMethodName, propertyData);
            var body          = Expression.Block(
                new[] { propertyData },
                Expression.IfThen(propertyExists, ExpressionUtils.SetProperty(instance, property, propertyValue)));

            return(Expression
                   .Lambda <TDelegate>(body, source, instance)
                   .Compile());
        }