Ejemplo n.º 1
0
        public static T Parse <T>(List <Parameter> descriptor, string path = "") where T : new()
        {
            var properties  = ElementsHelper.SortedProperties <T>();
            var result      = new T();
            var currentType = typeof(T);

            var thisMethod = typeof(ParametersConverter).GetMethod(nameof(Parse));

            if (!string.IsNullOrEmpty(path))
            {
                Logger.Trace("{0} '{1}'", path, currentType.Name);
            }
            foreach (var parameter in descriptor)
            {
                var currentPath = string.IsNullOrEmpty(path)
                    ? parameter.Id.ToString()
                    : string.Concat(path, '.', parameter.Id.ToString());
                if (!properties.ContainsKey(parameter.Id))
                {
                    throw new ArgumentException($"Parameter {parameter.Id} isn't supported for {currentType.Name}");
                }

                var propertyInfo = properties[parameter.Id];
                var propertyType = propertyInfo.PropertyType;

                if (propertyType == typeof(long) ||
                    propertyType == typeof(TextAlignment) ||
                    propertyType == typeof(bool) ||
                    propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, parameter.Value);
                    dynamic propertyValue = propertyInfo.GetValue(result, null);

                    if (propertyType.IsGenericType && propertyValue != null)
                    {
                        throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
                    }

                    if (!propertyType.IsGenericType && propertyType == typeof(long) && propertyValue != 0)
                    {
                        throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
                    }

                    if (propertyType == typeof(TextAlignment))
                    {
                        propertyInfo.SetValue(result, (TextAlignment)parameter.Value, null);
                    }
                    else if (propertyType == typeof(bool))
                    {
                        propertyInfo.SetValue(result, parameter.Value > 0, null);
                    }
                    else
                    {
                        propertyInfo.SetValue(result, parameter.Value, null);
                    }
                }
                else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    dynamic propertyValue = propertyInfo.GetValue(result, null);
                    if (propertyValue == null)
                    {
                        propertyValue = Activator.CreateInstance(propertyType);
                        propertyInfo.SetValue(result, propertyValue, null);
                    }

                    try
                    {
                        var     generic     = thisMethod.MakeGenericMethod(propertyType.GetGenericArguments()[0]);
                        dynamic parsedValue = generic.Invoke(null,
                                                             new dynamic[] { parameter.Children, currentPath });
                        propertyValue.Add(parsedValue);
                    }
                    catch (TargetInvocationException e)
                    {
                        throw e.InnerException;
                    }
                }
                else
                {
                    dynamic propertyValue = propertyInfo.GetValue(result, null);
                    if (propertyValue != null)
                    {
                        throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
                    }

                    try
                    {
                        var     generic     = thisMethod.MakeGenericMethod(propertyType);
                        dynamic parsedValue = generic.Invoke(null, new object[] { parameter.Children, currentPath });
                        propertyInfo.SetValue(result, parsedValue, null);
                    }
                    catch (TargetInvocationException e)
                    {
                        throw e.InnerException;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static T Parse <T>(List <Parameter> descriptor, string path = "") where T : new()
        {
            var properties  = ElementsHelper.SortedProperties <T>();
            var result      = new T();
            var currentType = typeof(T);

            var thisMethod = typeof(ParametersConverter).GetMethod(nameof(Parse));

            if (IgnoreError.GetDatai() == false)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("警告:道路千万条,安全第一条!");
                Console.ResetColor();
                Console.WriteLine("===============================");
                Console.WriteLine("是否忽略异常?(N/Y)默认否");
                Console.WriteLine("");
                Console.WriteLine("输入 Y ==>暴力解包,无视错误<=");
                Console.WriteLine("===============================");

                string i = Console.ReadLine();

                if (i == "Y")
                {
                    IgnoreError.Change(true);
                }
            }

            foreach (var parameter in descriptor)
            {
                var currentPath = string.IsNullOrEmpty(path)
                    ? parameter.Id.ToString()
                    : string.Concat(path, '.', parameter.Id.ToString());
                if (IgnoreError.GetDataIgnore() == false)
                {
                    if (!properties.ContainsKey(parameter.Id))
                    {
                        throw new ArgumentException($"参数 {parameter.Id} 不被 {currentType.Name} 支持");
                    }
                }
                var propertyInfo = properties[parameter.Id];
                var propertyType = propertyInfo.PropertyType;

                if (propertyType == typeof(long) ||
                    propertyType == typeof(TextAlignment) ||
                    propertyType == typeof(bool) ||
                    propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, parameter.Value);
                    dynamic propertyValue = propertyInfo.GetValue(result, null);

                    if (propertyType.IsGenericType && propertyValue != null)
                    {
                        throw new ArgumentException($"参数 {parameter.Id} 已经为 {currentType.Name} 设置");
                    }

                    if (!propertyType.IsGenericType && propertyType == typeof(long) && propertyValue != 0)
                    {
                        throw new ArgumentException($"参数 {parameter.Id} 已经为 {currentType.Name} 设置");
                    }

                    if (propertyType == typeof(TextAlignment))
                    {
                        propertyInfo.SetValue(result, (TextAlignment)parameter.Value, null);
                    }
                    else if (propertyType == typeof(bool))
                    {
                        propertyInfo.SetValue(result, parameter.Value > 0, null);
                    }
                    else
                    {
                        propertyInfo.SetValue(result, parameter.Value, null);
                    }
                }
                else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    Logger.Trace("{0} '{1}'", currentPath, propertyInfo.Name);
                    dynamic propertyValue = propertyInfo.GetValue(result, null);
                    if (propertyValue == null)
                    {
                        propertyValue = Activator.CreateInstance(propertyType);
                        propertyInfo.SetValue(result, propertyValue, null);
                    }

                    try
                    {
                        var     generic     = thisMethod.MakeGenericMethod(propertyType.GetGenericArguments()[0]);
                        dynamic parsedValue = generic.Invoke(null,
                                                             new dynamic[] { parameter.Children, currentPath });
                        propertyValue.Add(parsedValue);
                    }
                    catch (TargetInvocationException e)
                    {
                        if (IgnoreError.GetDataIgnore() == false)
                        {
                            throw e.InnerException;
                        }
                    }
                }
                else
                {
                    Logger.Trace("{0} '{1}'", currentPath, propertyInfo.Name);
                    dynamic propertyValue = propertyInfo.GetValue(result, null);
                    if (propertyValue != null)
                    {
                        throw new ArgumentException($"参数 {parameter.Id} 已经为 {currentType.Name} 设置");
                    }

                    try
                    {
                        var     generic     = thisMethod.MakeGenericMethod(propertyType);
                        dynamic parsedValue = generic.Invoke(null, new object[] { parameter.Children, currentPath });
                        propertyInfo.SetValue(result, parsedValue, null);
                    }
                    catch (TargetInvocationException e)
                    {
                        if (IgnoreError.GetDataIgnore() == false)
                        {
                            throw e.InnerException;
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static List <Parameter> Build <T>(T serializable, string path = "")
        {
            var result      = new List <Parameter>();
            var currentType = typeof(T);

            if (!string.IsNullOrEmpty(path))
            {
                Logger.Trace("{0} '{1}'", path, currentType.Name);
            }
            foreach (var kv in ElementsHelper.SortedProperties <T>())
            {
                var id          = kv.Key;
                var currentPath = string.IsNullOrEmpty(path)
                    ? id.ToString()
                    : string.Concat(path, '.', id.ToString());

                var     propertyInfo  = kv.Value;
                var     propertyType  = propertyInfo.PropertyType;
                dynamic propertyValue = propertyInfo.GetValue(serializable, null);

                if (propertyValue == null)
                {
                    continue;
                }

                if (propertyType == typeof(long) ||
                    propertyType == typeof(TextAlignment) ||
                    propertyType == typeof(bool))
                {
                    long value;
                    if (propertyType == typeof(bool))
                    {
                        value = propertyValue ? 1 : 0;
                    }
                    else
                    {
                        value = (long)propertyValue;
                    }

                    Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, value);
                    result.Add(new Parameter(id, value));
                }
                else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, propertyValue);
                    result.Add(new Parameter(id, propertyValue));
                }
                else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    foreach (var item in propertyValue)
                    {
                        result.Add(new Parameter(id, Build(item, currentPath)));
                    }
                }
                else
                {
                    var innerParameters = Build(propertyValue, currentPath);
                    if (innerParameters.Count > 0)
                    {
                        result.Add(new Parameter(id, innerParameters));
                    }
                    else
                    {
                        Logger.Trace("{0} '{1}': Skipped because of empty", currentPath, propertyInfo.Name);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        public void Process <T>(T serializable, string path = "")
        {
            if (!string.IsNullOrEmpty(path))
            {
                Logger.Trace("Loading images for {0} '{1}'", path, typeof(T).Name);
            }

            long?lastImageIndexValue = null;

            foreach (var kv in ElementsHelper.SortedProperties <T>())
            {
                var id          = kv.Key;
                var currentPath = string.IsNullOrEmpty(path)
                    ? id.ToString()
                    : string.Concat(path, '.', id.ToString());

                var     propertyInfo  = kv.Value;
                var     propertyType  = propertyInfo.PropertyType;
                dynamic propertyValue = propertyInfo.GetValue(serializable, null);

                var imageIndexAttribute =
                    ElementsHelper.GetCustomAttributeFor <ParameterImageIndexAttribute>(propertyInfo);
                var imagesCountAttribute =
                    ElementsHelper.GetCustomAttributeFor <ParameterImagesCountAttribute>(propertyInfo);

                if (imagesCountAttribute != null && imageIndexAttribute != null)
                {
                    throw new ArgumentException(
                              $"Property {propertyInfo.Name} can't have both ParameterImageIndexAttribute and ParameterImagesCountAttribute"
                              );
                }

                if (propertyType == typeof(long) || propertyType.IsGenericType &&
                    (propertyType.GetGenericTypeDefinition() == typeof(List <>) ||
                     propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                {
                    if (imageIndexAttribute != null)
                    {
                        if (propertyValue == null)
                        {
                            continue;
                        }
                        long imageIndex = propertyValue;

                        lastImageIndexValue = imageIndex;
                        var mappedIndex = LoadImage(imageIndex);
                        propertyInfo.SetValue(serializable, mappedIndex, null);
                    }
                    else if (imagesCountAttribute != null)
                    {
                        if (lastImageIndexValue == null)
                        {
                            throw new ArgumentException(
                                      $"Property {propertyInfo.Name} can't be processed becuase ImageIndex isn't present or it is zero"
                                      );
                        }

                        var imagesCount = propertyType.IsGenericType
                            ? (propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)
                                ? propertyValue.Value
                                : propertyValue.Count)
                            : propertyValue;

                        for (var i = lastImageIndexValue + 1; i < lastImageIndexValue + imagesCount; i++)
                        {
                            LoadImage(i.Value);
                        }
                    }
                }
                else
                {
                    if (imagesCountAttribute == null && imageIndexAttribute == null)
                    {
                        if (propertyValue != null)
                        {
                            Process(propertyValue, currentPath);
                        }
                    }
                    else
                    {
                        throw new ArgumentException(
                                  $"Property {propertyInfo.Name} with type {propertyType.Name} can't have ParameterImageIndexAttribute or ParameterImagesCountAttribute"
                                  );
                    }
                }
            }
        }