Index: src/Common/NetTopologySuite.Extensions/Features/FeatureAttributeAccessorHelper.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/NetTopologySuite.Extensions/Features/FeatureAttributeAccessorHelper.cs (.../FeatureAttributeAccessorHelper.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/NetTopologySuite.Extensions/Features/FeatureAttributeAccessorHelper.cs (.../FeatureAttributeAccessorHelper.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -16,24 +16,6 @@ private static readonly IDictionary> getterCache = new Dictionary>(); - private static Dictionary GetFeatureAttributes(Type featureType) - { - var result = new Dictionary(); - if (featureType == null) - { - return result; - } - foreach (var p in featureType.GetProperties()) - { - var attribute = p.GetCustomAttributes(true).OfType().FirstOrDefault(); - if (attribute != null) - { - result.Add(p, attribute); - } - } - return result; - } - public static IEnumerable GetFeatureAttributeNames(Type featureType) { return GetFeatureAttributes(featureType).OrderBy(kvp => kvp.Value.Order).Select(kvp => kvp.Key.Name); @@ -60,16 +42,6 @@ return attributeNames; } - private static void ThrowOnNotFound(Type featureType, string name) - { - throw new ArgumentException("Can't find attribute " + name + " for type " + featureType.Name); - } - - private static PropertyInfo GetFeatureProperty(Type featureType, string name) - { - return featureType == null ? null : featureType.GetProperties().FirstOrDefault(p => p.Name == name); - } - public static IEnumerable GetFeatureProperties(Type featureType) { return GetFeatureAttributes(featureType).OrderBy(kvp => kvp.Value.Order).Select(kvp => kvp.Key); @@ -88,41 +60,41 @@ { if (noDataValue == null) { - return (T) Activator.CreateInstance(typeof (T)); + return (T) Activator.CreateInstance(typeof(T)); } - return (T) Convert.ChangeType(noDataValue, typeof (T)); + return (T) Convert.ChangeType(noDataValue, typeof(T)); } - if (typeof (T) == typeof (string)) + if (typeof(T) == typeof(string)) { if (value.GetType().IsEnum) { return (T) (object) value.ToString(); } return (T) (object) string.Format("{0:g}", value); } - if (typeof (T) == typeof (double)) + if (typeof(T) == typeof(double)) { return (T) (object) Convert.ToDouble(value, CultureInfo.InvariantCulture); } - if (typeof (T) == typeof (int)) + if (typeof(T) == typeof(int)) { return (T) (object) Convert.ToInt32(value, CultureInfo.InvariantCulture); } - if (typeof (T) == typeof (short)) + if (typeof(T) == typeof(short)) { return (T) (object) Convert.ToInt16(value, CultureInfo.InvariantCulture); } - if (typeof (T) == typeof (float)) + if (typeof(T) == typeof(float)) { return (T) (object) Convert.ToSingle(value, CultureInfo.InvariantCulture); } - if (typeof (T) == typeof (byte)) + if (typeof(T) == typeof(byte)) { return (T) (object) Convert.ToByte(value, CultureInfo.InvariantCulture); } - if (typeof (T) == typeof (long)) + if (typeof(T) == typeof(long)) { return (T) (object) Convert.ToInt64(value, CultureInfo.InvariantCulture); } @@ -135,7 +107,7 @@ if (feature.Attributes != null) { object value; - if(feature.Attributes.TryGetValue(name, out value)) + if (feature.Attributes.TryGetValue(name, out value)) { return value; } @@ -186,7 +158,10 @@ var setter = propertyInfo.GetSetMethod(true); if (setter != null) { - setter.Invoke(feature, new[] {value}); + setter.Invoke(feature, new[] + { + value + }); return; } } @@ -224,44 +199,12 @@ return null; } - private static MethodInfo GetCachedAttributeValueGetter(IFeature feature, string name, bool throwIfNotFound = true) - { - MethodInfo getter; - IDictionary gettersForType; - - if (!getterCache.TryGetValue(feature.GetType(), out gettersForType)) - { - gettersForType = new Dictionary(); - getterCache.Add(feature.GetType(), gettersForType); - } - - if (!gettersForType.TryGetValue(name, out getter)) - { - getter = GetAttributeValueGetter(feature, name, throwIfNotFound); - gettersForType.Add(name, getter); - } - - return getter; - } - - private static MethodInfo GetAttributeValueGetter(IFeature feature, string name, bool throwIfNotFound) - { - var propertyInfo = GetFeatureProperty(feature.GetType(), name); - if (propertyInfo != null) - { - return propertyInfo.GetGetMethod(true); - } - if (throwIfNotFound) - { - ThrowOnNotFound(feature.GetType(), name); - } - return null; - } - public static string GetPropertyDisplayName(Type featureType, string name, bool throwIfNotFound = true) { if (featureType.Implements()) + { return name; + } var propertyInfo = GetFeatureProperty(featureType, name); @@ -295,27 +238,6 @@ return GetPropertyDisplayName(feature.GetType(), name, throwIfNotFound); } - private static bool GetAttributeReadonlyFlag(Type featureType, string name, bool throwIfNotFound = true) - { - if (featureType.Implements()) - return false; - - var propertyInfo = GetFeatureProperty(featureType, name); - - if (propertyInfo != null) - { - var readOnlyAttribute = - propertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault(); - - return readOnlyAttribute != null && readOnlyAttribute.IsReadOnly; - } - if (throwIfNotFound) - { - ThrowOnNotFound(featureType, name); - } - return false; - } - public static Type GetAttributeType(IFeature feature, string name, bool throwIfNotFound = true) { if (feature is DataRow) @@ -352,7 +274,7 @@ public static bool IsReadOnly(Type featureType, string name) { - if(featureType.GetProperty(name).GetSetMethod() == null) + if (featureType.GetProperty(name).GetSetMethod() == null) { return true; } @@ -369,9 +291,94 @@ } var formatAttribute = - propertyInfo.GetCustomAttributes(typeof (DisplayFormatAttribute), true).FirstOrDefault(); + propertyInfo.GetCustomAttributes(typeof(DisplayFormatAttribute), true).FirstOrDefault(); return formatAttribute != null ? ((DisplayFormatAttribute) formatAttribute).FormatString : ""; } + + private static Dictionary GetFeatureAttributes(Type featureType) + { + var result = new Dictionary(); + if (featureType == null) + { + return result; + } + foreach (var p in featureType.GetProperties()) + { + var attribute = p.GetCustomAttributes(true).OfType().FirstOrDefault(); + if (attribute != null) + { + result.Add(p, attribute); + } + } + return result; + } + + private static void ThrowOnNotFound(Type featureType, string name) + { + throw new ArgumentException("Can't find attribute " + name + " for type " + featureType.Name); + } + + private static PropertyInfo GetFeatureProperty(Type featureType, string name) + { + return featureType == null ? null : featureType.GetProperties().FirstOrDefault(p => p.Name == name); + } + + private static MethodInfo GetCachedAttributeValueGetter(IFeature feature, string name, bool throwIfNotFound = true) + { + MethodInfo getter; + IDictionary gettersForType; + + if (!getterCache.TryGetValue(feature.GetType(), out gettersForType)) + { + gettersForType = new Dictionary(); + getterCache.Add(feature.GetType(), gettersForType); + } + + if (!gettersForType.TryGetValue(name, out getter)) + { + getter = GetAttributeValueGetter(feature, name, throwIfNotFound); + gettersForType.Add(name, getter); + } + + return getter; + } + + private static MethodInfo GetAttributeValueGetter(IFeature feature, string name, bool throwIfNotFound) + { + var propertyInfo = GetFeatureProperty(feature.GetType(), name); + if (propertyInfo != null) + { + return propertyInfo.GetGetMethod(true); + } + if (throwIfNotFound) + { + ThrowOnNotFound(feature.GetType(), name); + } + return null; + } + + private static bool GetAttributeReadonlyFlag(Type featureType, string name, bool throwIfNotFound = true) + { + if (featureType.Implements()) + { + return false; + } + + var propertyInfo = GetFeatureProperty(featureType, name); + + if (propertyInfo != null) + { + var readOnlyAttribute = + propertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault(); + + return readOnlyAttribute != null && readOnlyAttribute.IsReadOnly; + } + if (throwIfNotFound) + { + ThrowOnNotFound(featureType, name); + } + return false; + } } } \ No newline at end of file