Ejemplo n.º 1
0
        private void SetDictionary()
        {
            var properties = typeof(T).GetRuntimeProperties();

            foreach (var property in properties)
            {
                var element = (BinaryElement)property.GetCustomAttribute(typeof(BinaryElement));
                if (element != null)
                {
                    if (element.Direction == DirectionEnum.Reverse)
                    {
                        if (_bytes == null)
                        {
                            element.FieldOffset = 1000;
                        }
                        else
                        {
                            element.FieldOffset = _bytes.Length - element.Length - element.FieldOffset;
                        }
                    }
                    var el = new SerializeInfoElement()
                    {
                        Element = element, Info = property
                    };

                    InfoElements.Add(property.Name, el);
                }
                PropertyInfos.Add(property);
            }

            InfoElementOrderedList.AddRange(InfoElements.Select(e => e.Value).OrderBy(e => e.Element.FieldOffset));
        }
Ejemplo n.º 2
0
 public AttributeGetter(Type type)
 {
     type = type ?? throw new ArgumentNullException("type");
     foreach (PropertyInfo propInfo in type.GetProperties())
     {
         PropertyInfos.Add(propInfo.Name, propInfo);
     }
 }
Ejemplo n.º 3
0
 public string this[string propertyName]
 {
     get
     {
         var propertyInfo = this.GetType().GetRuntimeProperty(propertyName);
         PropertyInfos.Add(propertyInfo);
         return(string.Empty);
     }
 }
Ejemplo n.º 4
0
 public void AddOrUpdatePropertyInfos(ICollection <PropertyInfo> aPropertyInfoCollection)
 {
     if (aPropertyInfoCollection != null && aPropertyInfoCollection.Count > 0)
     {
         foreach (var item in aPropertyInfoCollection)
         {
             var existItem = PropertyInfos.FirstOrDefault(p => p.UId == item.UId);
             if (existItem == null)
             {
                 PropertyInfos.Add(item);
             }
             else
             {
                 existItem.Update(item.PropertyId, item.PropertyValueIds);
             }
         }
     }
 }
Ejemplo n.º 5
0
        internal static List <IPatchBase> ReadPatchDocumentProperties <T>(JsonReader reader, string path,
                                                                          BindAttribute bindAttribute        = null, Action <JsonReader> patchTypeAction = null,
                                                                          Action <IPatchPrimitive> keyAction = null)
        {
            if (reader == null)
            {
                return(null);
            }

            var          modelTypeAssemblyName = typeof(T).AssemblyQualifiedName;
            bool         cacheProperties;
            const string patchTypeKey = PatchArrayDocument <object> .PatchTypeKey;

            // ReSharper disable once AssignmentInConditionalExpression
            var modelProperties = (cacheProperties = PropertyInfos.All(x => x.Key.Key1 != modelTypeAssemblyName))
                ? typeof(T).GetTypeInfo().GetProperties()
                : PropertyInfos.Where(x => x.Key.Key1 == modelTypeAssemblyName).Select(x => x.Value).ToArray();

            var values    = new List <IPatchBase>();
            var startPath = reader.Path;

            bindAttribute = bindAttribute ?? typeof(T).GetAttribute <BindAttribute>();
            path          = string.IsNullOrWhiteSpace(path) ? string.Empty : path + ".";

            var keys = new List <IPatchPrimitive>();

            while (reader.Read())
            {
                if (reader.Path == startPath)
                {
                    break;
                }

                if (reader.TokenType != JsonToken.PropertyName)
                {
                    while (reader.Read())
                    {
                        if (reader.Path == startPath)
                        {
                            return(null);
                        }
                    }
                }

                var readerValueAsName = (string)reader.Value;

                if (readerValueAsName == patchTypeKey)
                {
                    if (patchTypeAction != null)
                    {
                        patchTypeAction(reader);
                    }
                    else
                    {
                        reader.Skip();
                    }

                    continue;
                }

                var propertyInfo = modelProperties.FirstOrDefault(x =>
                {
                    var jsonPropertyNameAttribute = x.GetCustomAttribute <JsonPropertyAttribute>();

                    if (jsonPropertyNameAttribute != null)
                    {
                        return(string.Equals(jsonPropertyNameAttribute.PropertyName, readerValueAsName));
                    }

                    return(readerValueAsName.Equals(new CamelCaseNamingStrategy().GetPropertyName(x.Name, false)) ||
                           readerValueAsName.Equals(x.Name) ||
                           readerValueAsName.Equals(new SnakeCaseNamingStrategy().GetPropertyName(x.Name, false)));
                });

                // If property not found, we should report an error with it.
                if (propertyInfo == null)
                {
                    values.Add(new PatchPrimitive <object>
                    {
                        Errors = new List <Error>
                        {
                            new Error
                            {
                                ErrorType = typeof(InvalidCastException),
                                JsonName  = readerValueAsName,
                                JsonPath  = reader.Path,
                                Message   = string.Format(Resources.UnknownProperty, readerValueAsName),
                                Name      = readerValueAsName,
                                Path      = reader.Path
                            }
                        },
                        Found    = true,
                        JsonName = readerValueAsName,
                        JsonPath = reader.Path,
                        Name     = readerValueAsName,
                        Path     = reader.Path,
                        Value    = null
                    });

                    // Since this is an unknown property, we should not parse it.
                    reader.Skip();

                    continue;
                }

                var propertyName = propertyInfo.Name;
                var isKey        = propertyInfo.GetCustomAttribute <KeyAttribute>() != null;
                var isBindable   = IsBindable(bindAttribute, propertyName);

                if (!isBindable && (!isKey || keyAction == null))
                {
                    reader.Skip();
                    continue;
                }

                var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType)
                            .Invoke(null, new object[] { reader, propertyName, path + propertyName, propertyInfo });

                value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null;

                if (isKey && keyAction != null)
                {
                    keys.Add((IPatchPrimitive)value);
                }

                if (isBindable)
                {
                    values.Add(value);
                }
            }

            foreach (var propertyInfo in modelProperties)
            {
                if (cacheProperties)
                {
                    PropertyInfos.Add(modelTypeAssemblyName, propertyInfo.Name, propertyInfo);
                }

                if (values.Any(x => x.Name == propertyInfo.Name))
                {
                    continue;
                }

                var propertyName = propertyInfo.Name;
                var isKey        = propertyInfo.GetCustomAttribute <KeyAttribute>() != null;
                var isBindable   = IsBindable(bindAttribute, propertyName);

                if (!isBindable && (!isKey || keyAction == null))
                {
                    continue;
                }

                var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType)
                            .Invoke(null, new object[] { null, propertyName, path + propertyName, propertyInfo });

                value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null;

                if (isKey && keyAction != null && keys.All(x => x.Name != value.Name))
                {
                    keys.Add((IPatchPrimitive)value);
                }

                if (isBindable)
                {
                    values.Add(value);
                }
            }

            if (keys.Any())
            {
                foreach (var key in keys)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    keyAction.Invoke(key);
                }
            }

            return(values);
        }