Esempio n. 1
0
        public override Newtonsoft.Json.Serialization.Func <T, object?> CreateGet <T>(PropertyInfo propertyInfo)
        {
            ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));

            Type instanceType = typeof(T);
            Type resultType   = typeof(object);

            ParameterExpression parameterExpression = Expression.Parameter(instanceType, "instance");
            Expression          resultExpression;

            MethodInfo?getMethod = propertyInfo.GetGetMethod(true);

            if (getMethod == null)
            {
                throw new ArgumentException("Property does not have a getter.");
            }

            if (getMethod.IsStatic)
            {
                resultExpression = Expression.MakeMemberAccess(null, propertyInfo);
            }
            else
            {
                Expression readParameter = EnsureCastExpression(parameterExpression, propertyInfo.DeclaringType);

                resultExpression = Expression.MakeMemberAccess(readParameter, propertyInfo);
            }

            resultExpression = EnsureCastExpression(resultExpression, resultType);

            LambdaExpression lambdaExpression = Expression.Lambda(typeof(Newtonsoft.Json.Serialization.Func <T, object>), resultExpression, parameterExpression);

            Newtonsoft.Json.Serialization.Func <T, object?> compiled = (Newtonsoft.Json.Serialization.Func <T, object?>)lambdaExpression.Compile();
            return(compiled);
        }
Esempio n. 2
0
        public override Newtonsoft.Json.Serialization.Func <T> CreateDefaultConstructor <T>(Type type)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            // avoid error from expressions compiler because of abstract class
            if (type.IsAbstract())
            {
                return(() => (T)Activator.CreateInstance(type));
            }

            try
            {
                Type resultType = typeof(T);

                Expression expression = Expression.New(type);

                expression = EnsureCastExpression(expression, resultType);

                LambdaExpression lambdaExpression = Expression.Lambda(typeof(Newtonsoft.Json.Serialization.Func <T>), expression);

                Newtonsoft.Json.Serialization.Func <T> compiled = (Newtonsoft.Json.Serialization.Func <T>)lambdaExpression.Compile();
                return(compiled);
            }
            catch
            {
                // an error can be thrown if constructor is not valid on Win8
                // will have INVOCATION_FLAGS_NON_W8P_FX_API invocation flag
                return(() => (T)Activator.CreateInstance(type));
            }
        }
        public ThreadSafeStore(Newtonsoft.Json.Serialization.Func <TKey, TValue> creator)
        {
            ValidationUtils.ArgumentNotNull(creator, nameof(creator));

            _creator = creator;
#if HAVE_CONCURRENT_DICTIONARY
            _concurrentStore = new ConcurrentDictionary <TKey, TValue>();
#else
            _store = new Dictionary <TKey, TValue>();
#endif
        }
Esempio n. 4
0
        public static int IndexOf <T>(this IEnumerable <T> collection, Newtonsoft.Json.Serialization.Func <T, bool> predicate)
        {
            int index = 0;

            foreach (T value in collection)
            {
                if (predicate(value))
                {
                    return(index);
                }

                index++;
            }

            return(-1);
        }
        public override Newtonsoft.Json.Serialization.Func <T, object?> CreateGet <T>(FieldInfo fieldInfo)
        {
            if (fieldInfo.IsLiteral)
            {
                object constantValue = fieldInfo.GetValue(null);
                Newtonsoft.Json.Serialization.Func <T, object?> getter = o => constantValue;
                return(getter);
            }

            DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + fieldInfo.Name, typeof(T), new[] { typeof(object) }, fieldInfo.DeclaringType);
            ILGenerator   generator     = dynamicMethod.GetILGenerator();

            GenerateCreateGetFieldIL(fieldInfo, generator);

            return((Newtonsoft.Json.Serialization.Func <T, object?>)dynamicMethod.CreateDelegate(typeof(Newtonsoft.Json.Serialization.Func <T, object?>)));
        }
Esempio n. 6
0
        public override Newtonsoft.Json.Serialization.Func <T, object?> CreateGet <T>(FieldInfo fieldInfo)
        {
            ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo));

            ParameterExpression sourceParameter = Expression.Parameter(typeof(T), "source");

            Expression fieldExpression;

            if (fieldInfo.IsStatic)
            {
                fieldExpression = Expression.Field(null, fieldInfo);
            }
            else
            {
                Expression sourceExpression = EnsureCastExpression(sourceParameter, fieldInfo.DeclaringType);

                fieldExpression = Expression.Field(sourceExpression, fieldInfo);
            }

            fieldExpression = EnsureCastExpression(fieldExpression, typeof(object));

            Newtonsoft.Json.Serialization.Func <T, object?> compiled = Expression.Lambda <Newtonsoft.Json.Serialization.Func <T, object?> >(fieldExpression, sourceParameter).Compile();
            return(compiled);
        }
Esempio n. 7
0
        public static TSource ForgivingCaseSensitiveFind <TSource>(this IEnumerable <TSource> source, Newtonsoft.Json.Serialization.Func <TSource, string> valueSelector, string testValue)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (valueSelector == null)
            {
                throw new ArgumentNullException(nameof(valueSelector));
            }

            IEnumerable <TSource> caseInsensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase));

            if (caseInsensitiveResults.Count() <= 1)
            {
                return(caseInsensitiveResults.SingleOrDefault());
            }
            else
            {
                // multiple results returned. now filter using case sensitivity
                IEnumerable <TSource> caseSensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.Ordinal));
                return(caseSensitiveResults.SingleOrDefault());
            }
        }
Esempio n. 8
0
        public static ReflectionObject Create(Type t, MethodBase?creator, params string[] memberNames)
        {
            ReflectionDelegateFactory delegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            ObjectConstructor <object>?creatorConstructor = null;

            if (creator != null)
            {
                creatorConstructor = delegateFactory.CreateParameterizedConstructor(creator);
            }
            else
            {
                if (ReflectionUtils.HasDefaultConstructor(t, false))
                {
                    Newtonsoft.Json.Serialization.Func <object> ctor = delegateFactory.CreateDefaultConstructor <object>(t);

                    creatorConstructor = args => ctor();
                }
            }

            ReflectionObject d = new ReflectionObject(creatorConstructor);

            foreach (string memberName in memberNames)
            {
                MemberInfo[] members = t.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public);
                if (members.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, memberName));
                }

                MemberInfo member = members.Single();

                ReflectionMember reflectionMember = new ReflectionMember();

                switch (member.MemberType())
                {
                case MemberTypes.Field:
                case MemberTypes.Property:
                    if (ReflectionUtils.CanReadMemberValue(member, false))
                    {
                        reflectionMember.Getter = delegateFactory.CreateGet <object>(member);
                    }

                    if (ReflectionUtils.CanSetMemberValue(member, false, false))
                    {
                        reflectionMember.Setter = delegateFactory.CreateSet <object>(member);
                    }
                    break;

                case MemberTypes.Method:
                    MethodInfo method = (MethodInfo)member;
                    if (method.IsPublic)
                    {
                        ParameterInfo[] parameters = method.GetParameters();
                        if (parameters.Length == 0 && method.ReturnType != typeof(void))
                        {
                            MethodCall <object, object?> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Getter = target => call(target);
                        }
                        else if (parameters.Length == 1 && method.ReturnType == typeof(void))
                        {
                            MethodCall <object, object?> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Setter = (target, arg) => call(target, arg);
                        }
                    }
                    break;

                default:
                    throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith(CultureInfo.InvariantCulture, member.MemberType(), member.Name));
                }

                reflectionMember.MemberType = ReflectionUtils.GetMemberUnderlyingType(member);

                d.Members[memberName] = reflectionMember;
            }

            return(d);
        }
Esempio n. 9
0
 public object?GetValue(object target, string member)
 {
     Newtonsoft.Json.Serialization.Func <object, object?> getter = Members[member].Getter !;
     return(getter(target));
 }