Esempio n. 1
0
        private void ProcessShouldSerializeMethod <T>(MemberMapping <T> memberMapping)
        {
            string shouldSerializeMethodName = "ShouldSerialize" + memberMapping.MemberInfo.Name;
            Type?  objectType = memberMapping.MemberInfo.DeclaringType;

            if (objectType == null)
            {
                return;
            }

            MethodInfo?shouldSerializeMethodInfo = objectType.GetMethod(shouldSerializeMethodName, new Type[] { });

            if (shouldSerializeMethodInfo != null &&
                shouldSerializeMethodInfo.IsPublic &&
                shouldSerializeMethodInfo.ReturnType == typeof(bool))
            {
                // obj => ((TClass) obj).ShouldSerializeXyz()
                ParameterExpression objParameter = Expression.Parameter(typeof(object), "obj");
                Expression <Func <object, bool> > lambdaExpression = Expression.Lambda <Func <object, bool> >(
                    Expression.Call(
                        Expression.Convert(objParameter, objectType),
                        shouldSerializeMethodInfo),
                    objParameter);

                memberMapping.SetShouldSerializeMethod(lambdaExpression.Compile());
            }
        }
Esempio n. 2
0
        private void ProcessShouldSerializeMethod <T>(MemberMapping <T> memberMapping)
        {
            string shouldSerializeMethodName = "ShouldSerialize" + memberMapping.MemberInfo.Name;
            Type   objectType = typeof(T);

            MethodInfo?shouldSerializeMethodInfo = objectType.GetMethod(
                shouldSerializeMethodName,
                BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance,
                null, new Type[] { }, null);

            if (shouldSerializeMethodInfo != null &&
                shouldSerializeMethodInfo.ReturnType == typeof(bool))
            {
                // obj => ((TClass) obj).ShouldSerializeXyz()
                ParameterExpression objParameter = Expression.Parameter(typeof(object), "obj");
                Expression <Func <object, bool> > lambdaExpression = Expression.Lambda <Func <object, bool> >(
                    Expression.Call(
                        Expression.Convert(objParameter, objectType),
                        shouldSerializeMethodInfo),
                    objParameter);

                memberMapping.SetShouldSerializeMethod(lambdaExpression.Compile());
            }
        }