private static Dictionary <Tuple <string, string>, MemberInfo> GetPropertiesCache(Type targetType)
            {
                if (TypeCache.ContainsKey(targetType))
                {
                    return(TypeCache[targetType]);
                }

                var fields = new List <MemberInfo>();

                // If the target is a struct (value type) navigate the fields.
                if (targetType.IsValueType())
                {
                    fields.AddRange(FieldTypeCache.RetrieveAllFields(targetType));
                }

                // then incorporate the properties
                fields.AddRange(PropertyTypeCache.RetrieveAllProperties(targetType).Where(p => p.CanRead));

                TypeCache[targetType] = fields
                                        .ToDictionary(
                    x => new Tuple <string, string>(x.Name,
                                                    x.GetCustomAttribute <JsonPropertyAttribute>()?.PropertyName ?? x.Name),
                    x => x);

                return(TypeCache[targetType]);
            }
Example #2
0
            private void PopulateFields(Dictionary <string, object> sourceProperties)
            {
                foreach (var field in FieldTypeCache.RetrieveAllFields(_targetType))
                {
                    var sourcePropertyValue = GetSourcePropertyValue(sourceProperties, field);
                    if (sourcePropertyValue == null)
                    {
                        continue;
                    }

                    var targetPropertyValue = FromJsonResult(
                        sourcePropertyValue,
                        field.FieldType,
                        _includeNonPublic);

                    try
                    {
                        field.SetValue(_target, targetPropertyValue);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
Example #3
0
            private static Dictionary <Tuple <string, string>, MemberInfo> GetPropertiesCache(Type targetType)
            {
                if (TypeCache.TryGetValue(targetType, out var current))
                {
                    return(current);
                }

                var fields =
                    new List <MemberInfo>(PropertyTypeCache.RetrieveAllProperties(targetType).Where(p => p.CanRead));

                // If the target is a struct (value type) navigate the fields.
                if (targetType.IsValueType())
                {
                    fields.AddRange(FieldTypeCache.RetrieveAllFields(targetType));
                }

                var value = fields
                            .ToDictionary(
                    x => Tuple.Create(x.Name,
                                      x.GetCustomAttribute <JsonPropertyAttribute>()?.PropertyName ?? x.Name),
                    x => x);

                TypeCache.TryAdd(targetType, value);

                return(value);
            }