Возвращает истину.
Код для тестирования:
Код | using System; using System.Reflection;
class Program { static void Main(string[] args) { WriteInfo(typeof(MyClass3), "Property1"); Console.ReadKey(); }
private static void WriteInfo(Type type, string name) { BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; PropertyInfo prop = type.GetProperty(name, flags); if (prop == null) return;
Console.WriteLine("Класс: {0}", type); Console.WriteLine("Свойство: {0}", prop); Console.WriteLine("Declaring Type: {0}", prop.DeclaringType); Console.WriteLine("Reflected Type: {0}", prop.ReflectedType);
MethodInfo getter = prop.GetGetMethod(true); MethodInfo setter = prop.GetSetMethod(true);
Console.WriteLine("Аттрибуты: {0}", prop.Attributes); if (getter != null) Console.WriteLine("Аттрибуты get: {0}", getter.Attributes); if (setter != null) Console.WriteLine("Аттрибуты set: {0}", setter.Attributes); Console.WriteLine("Перегруженный: {0}", IsOverride(prop)); Console.WriteLine("Новый: {0}", !IsOverride(prop));
Console.WriteLine(); }
private static bool IsOverride(PropertyInfo prop) { MethodInfo method = prop.CanRead ? prop.GetGetMethod() : prop.GetSetMethod(); MethodAttributes vtable = method.Attributes & MethodAttributes.NewSlot; MethodAttributes virt = method.Attributes & MethodAttributes.Virtual; return vtable != MethodAttributes.NewSlot && virt == MethodAttributes.Virtual; } }
|
Результат:
Код | Класс: MyClass3 Свойство: Int32 Property1 Declaring Type: MyClass3 Reflected Type: MyClass3 Аттрибуты: None Аттрибуты get: PrivateScope, Public, Virtual, HideBySig, SpecialName Аттрибуты set: PrivateScope, Public, Virtual, HideBySig, SpecialName Перегруженный: True // что нам и надо было найти Новый: False
|
Добавлено через 5 минут и 26 секунд А на счет PropertyInfo.GetOptionalCustomModifiers() (а также PropertyInfo.GetRequiredCustomModifiers()), то здесь не все ясно. У меня они всегда возвращают пустой массив. |