diff --git a/Resources/Resources.csproj b/Resources/Resources.csproj index 5c0c5fe..78dc200 100644 --- a/Resources/Resources.csproj +++ b/Resources/Resources.csproj @@ -9,8 +9,9 @@ Properties Resources Resources - v4.6.1 + v4.0 512 + Client true @@ -31,7 +32,7 @@ - ..\packages\NLog.4.4.12\lib\net45\NLog.dll + ..\packages\NLog.4.4.12\lib\net40\NLog.dll @@ -40,7 +41,6 @@ - diff --git a/Resources/packages.config b/Resources/packages.config index e50f18a..84cdf27 100644 --- a/Resources/packages.config +++ b/Resources/packages.config @@ -1,5 +1,4 @@  - - + \ No newline at end of file diff --git a/WatchFace.Parser/Reader.cs b/WatchFace.Parser/Reader.cs index ccb6a1b..1235920 100644 --- a/WatchFace.Parser/Reader.cs +++ b/WatchFace.Parser/Reader.cs @@ -50,8 +50,7 @@ namespace WatchFace.Parser Images = new ResourcesReader(_stream).Read((uint) imagesCount); } - private List ReadParameters(long coordinatesTableSize, - IReadOnlyCollection parametersDescriptors) + private List ReadParameters(long coordinatesTableSize, ICollection parametersDescriptors) { var parametersStream = StreamBlock(_stream, (int) coordinatesTableSize); diff --git a/WatchFace.Parser/Utils/ElementsHelper.cs b/WatchFace.Parser/Utils/ElementsHelper.cs new file mode 100644 index 0000000..9f5003a --- /dev/null +++ b/WatchFace.Parser/Utils/ElementsHelper.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using WatchFace.Parser.Attributes; + +namespace WatchFace.Parser.Utils +{ + internal class ElementsHelper + { + public static Dictionary SortedProperties() + { + var typeInfo = typeof(T); + var properties = new Dictionary(); + foreach (var propertyInfo in typeInfo.GetProperties()) + { + var parameterIdAttribute = (ParameterIdAttribute) propertyInfo + .GetCustomAttributes(typeof(ParameterIdAttribute), false).FirstOrDefault(); + if (parameterIdAttribute == null) + throw new ArgumentException( + $"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}" + ); + if (properties.ContainsKey(parameterIdAttribute.Id)) + throw new ArgumentException( + $"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}" + ); + + properties[parameterIdAttribute.Id] = propertyInfo; + } + return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value); + } + + public static T GetCustomAttributeFor(PropertyInfo propertyInfo) + { + return (T) propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault(); + } + } +} \ No newline at end of file diff --git a/WatchFace.Parser/Utils/ImagesLoader.cs b/WatchFace.Parser/Utils/ImagesLoader.cs index 1fc93d8..b3b1efc 100644 --- a/WatchFace.Parser/Utils/ImagesLoader.cs +++ b/WatchFace.Parser/Utils/ImagesLoader.cs @@ -31,7 +31,7 @@ namespace WatchFace.Parser.Utils long? lastImageIndexValue = null; - foreach (var kv in SortedPropertiesDictionary()) + foreach (var kv in ElementsHelper.SortedProperties()) { var id = kv.Key; var currentPath = string.IsNullOrEmpty(path) @@ -42,12 +42,8 @@ namespace WatchFace.Parser.Utils var propertyType = propertyInfo.PropertyType; dynamic propertyValue = propertyInfo.GetValue(serializable, null); - var imageIndexAttribute = (ParameterImageIndexAttribute) propertyInfo.GetCustomAttribute( - typeof(ParameterImageIndexAttribute) - ); - var imagesCountAttribute = (ParameterImagesCountAttribute) propertyInfo.GetCustomAttribute( - typeof(ParameterImagesCountAttribute) - ); + var imageIndexAttribute =ElementsHelper.GetCustomAttributeFor(propertyInfo); + var imagesCountAttribute =ElementsHelper.GetCustomAttributeFor(propertyInfo); if (imagesCountAttribute != null && imageIndexAttribute != null) throw new ArgumentException( @@ -65,7 +61,7 @@ namespace WatchFace.Parser.Utils lastImageIndexValue = imageIndex; var mappedIndex = LoadImage(imageIndex); - propertyInfo.SetValue(serializable, mappedIndex); + propertyInfo.SetValue(serializable, mappedIndex, null); } else if (imagesCountAttribute != null) { @@ -112,27 +108,5 @@ namespace WatchFace.Parser.Utils _mapping[index] = newImageIndex; return newImageIndex; } - - private static Dictionary SortedPropertiesDictionary() - { - var typeInfo = typeof(T).GetTypeInfo(); - var properties = new Dictionary(); - foreach (var propertyInfo in typeInfo.DeclaredProperties) - { - var parameterIdAttribute = - (ParameterIdAttribute) propertyInfo.GetCustomAttribute(typeof(ParameterIdAttribute)); - if (parameterIdAttribute == null) - throw new ArgumentException( - $"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}" - ); - if (properties.ContainsKey(parameterIdAttribute.Id)) - throw new ArgumentException( - $"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}" - ); - - properties[parameterIdAttribute.Id] = propertyInfo; - } - return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value); - } } } \ No newline at end of file diff --git a/WatchFace.Parser/Utils/ParametersConverter.cs b/WatchFace.Parser/Utils/ParametersConverter.cs index 1aa20ad..dee8255 100644 --- a/WatchFace.Parser/Utils/ParametersConverter.cs +++ b/WatchFace.Parser/Utils/ParametersConverter.cs @@ -19,7 +19,7 @@ namespace WatchFace.Parser.Utils if (!string.IsNullOrEmpty(path)) Logger.Trace("{0} '{1}'", path, currentType.Name); - foreach (var kv in SortedPropertiesDictionary()) + foreach (var kv in ElementsHelper.SortedProperties()) { var id = kv.Key; var currentPath = string.IsNullOrEmpty(path) @@ -58,7 +58,7 @@ namespace WatchFace.Parser.Utils public static T Parse(List descriptor, string path = "") where T : new() { - var properties = SortedPropertiesDictionary(); + var properties = ElementsHelper.SortedProperties(); var result = new T(); var currentType = typeof(T); @@ -81,7 +81,7 @@ namespace WatchFace.Parser.Utils propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, parameter.Value); - dynamic propertyValue = propertyInfo.GetValue(result); + dynamic propertyValue = propertyInfo.GetValue(result, null); if (propertyType.IsGenericType && propertyValue != null) throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); @@ -89,15 +89,15 @@ namespace WatchFace.Parser.Utils if (!propertyType.IsGenericType && propertyValue != 0) throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); - propertyInfo.SetValue(result, parameter.Value); + propertyInfo.SetValue(result, parameter.Value, null); } else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>)) { - dynamic propertyValue = propertyInfo.GetValue(result); + dynamic propertyValue = propertyInfo.GetValue(result, null); if (propertyValue == null) { propertyValue = Activator.CreateInstance(propertyType); - propertyInfo.SetValue(result, propertyValue); + propertyInfo.SetValue(result, propertyValue, null); } try @@ -114,7 +114,7 @@ namespace WatchFace.Parser.Utils } else { - dynamic propertyValue = propertyInfo.GetValue(result); + dynamic propertyValue = propertyInfo.GetValue(result, null); if (propertyValue != null) throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); @@ -122,7 +122,7 @@ namespace WatchFace.Parser.Utils { var generic = thisMethod.MakeGenericMethod(propertyType); dynamic parsedValue = generic.Invoke(null, new object[] {parameter.Children, currentPath}); - propertyInfo.SetValue(result, parsedValue); + propertyInfo.SetValue(result, parsedValue, null); } catch (TargetInvocationException e) { @@ -132,27 +132,5 @@ namespace WatchFace.Parser.Utils } return result; } - - private static Dictionary SortedPropertiesDictionary() - { - var typeInfo = typeof(T).GetTypeInfo(); - var properties = new Dictionary(); - foreach (var propertyInfo in typeInfo.DeclaredProperties) - { - var parameterIdAttribute = - (ParameterIdAttribute) propertyInfo.GetCustomAttribute(typeof(ParameterIdAttribute)); - if (parameterIdAttribute == null) - throw new ArgumentException( - $"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}" - ); - if (properties.ContainsKey(parameterIdAttribute.Id)) - throw new ArgumentException( - $"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}" - ); - - properties[parameterIdAttribute.Id] = propertyInfo; - } - return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value); - } } } \ No newline at end of file diff --git a/WatchFace.Parser/WatchFace.Parser.csproj b/WatchFace.Parser/WatchFace.Parser.csproj index a78a9d2..a539cf8 100644 --- a/WatchFace.Parser/WatchFace.Parser.csproj +++ b/WatchFace.Parser/WatchFace.Parser.csproj @@ -9,8 +9,9 @@ Properties WatchFace.Parser WatchFace.Parser - v4.6.1 + v4.0 512 + Client true @@ -31,10 +32,10 @@ - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll - ..\packages\NLog.4.4.12\lib\net45\NLog.dll + ..\packages\NLog.4.4.12\lib\net40\NLog.dll @@ -82,6 +83,7 @@ + @@ -89,14 +91,14 @@ - - - {edd55d5d-9e80-451b-ac8a-0746ba6dc6e9} Resources + + + \ No newline at end of file diff --git a/WatchFace.Parser/packages.config b/WatchFace.Parser/packages.config index 7cb1b4e..0cd68dd 100644 --- a/WatchFace.Parser/packages.config +++ b/WatchFace.Parser/packages.config @@ -1,6 +1,5 @@  - - - + + \ No newline at end of file diff --git a/WatchFace/App.config b/WatchFace/App.config index efb4d02..088ab21 100644 --- a/WatchFace/App.config +++ b/WatchFace/App.config @@ -1,7 +1,6 @@ - - + - + - \ No newline at end of file + diff --git a/WatchFace/WatchFace.csproj b/WatchFace/WatchFace.csproj index 798382d..652baca 100644 --- a/WatchFace/WatchFace.csproj +++ b/WatchFace/WatchFace.csproj @@ -8,11 +8,12 @@ Exe WatchFace WatchFace - v4.6.1 + v4.0 512 true + Client AnyCPU @@ -35,10 +36,10 @@ - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll - ..\packages\NLog.4.4.12\lib\net45\NLog.dll + ..\packages\NLog.4.4.12\lib\net40\NLog.dll diff --git a/WatchFace/packages.config b/WatchFace/packages.config index 7cb1b4e..0cd68dd 100644 --- a/WatchFace/packages.config +++ b/WatchFace/packages.config @@ -1,6 +1,5 @@  - - - + + \ No newline at end of file