From 2d9961a8fff27c0c86b99731e69fd8e81ede6e8a Mon Sep 17 00:00:00 2001 From: "harald.martini@student.tugraz.at" <harald.martini@student.tugraz.at> Date: Mon, 2 Aug 2021 16:21:30 +0200 Subject: [PATCH] optimized usage of Reflection in ValidationHelper --- VectoCommon/VectoCommon/Utils/Validation.cs | 32 ++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/VectoCommon/VectoCommon/Utils/Validation.cs b/VectoCommon/VectoCommon/Utils/Validation.cs index ebfe5f6b5d..0f8a09e0fa 100644 --- a/VectoCommon/VectoCommon/Utils/Validation.cs +++ b/VectoCommon/VectoCommon/Utils/Validation.cs @@ -114,19 +114,31 @@ namespace TUGraz.VectoCommon.Utils const BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy; - var prop = obj.GetProperty(m.Name, flags); - if (prop != null) { - attributes = prop.GetCustomAttributes(typeof(T)) - .Cast<T>() - .Concat(obj.GetInterfaces().SelectMany(m.GetAttributes<T>)); + + if (m.MemberType == MemberTypes.Property) { + PropertyInfo prop = m as PropertyInfo ?? obj.GetProperty(m.Name, flags); + //var prop = obj.GetProperty(m.Name, flags); + if (prop != null) + { + //if (m is PropertyInfo prop) { + attributes = prop.GetCustomAttributes(typeof(T)) + .Cast<T>() + .Concat(obj.GetInterfaces().SelectMany(m.GetAttributes<T>)); + } } - var field = obj.GetField(m.Name, flags); - if (field != null) { - attributes = - attributes.Concat( - field.GetCustomAttributes(typeof(T)).Cast<T>().Concat(obj.GetInterfaces().SelectMany(m.GetAttributes<T>))); + if (m.MemberType == MemberTypes.Field) { + FieldInfo field = m as FieldInfo ?? obj.GetField(m.Name, flags); + //var field = obj.GetField(m.Name, flags); + if (field != null) + { + //if (m is FieldInfo field) { + attributes = + attributes.Concat( + field.GetCustomAttributes(typeof(T)).Cast<T>().Concat(obj.GetInterfaces().SelectMany(m.GetAttributes<T>))); + } } + return attributes; } -- GitLab