Add IsCollectible property to Memberinfo and MethodInfo (#21155)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / shared / System / Reflection / MemberInfo.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Collections.Generic;
6
7 namespace System.Reflection
8 {
9     public abstract partial class MemberInfo : ICustomAttributeProvider
10     {
11         protected MemberInfo() { }
12
13         public abstract MemberTypes MemberType { get; }
14         public abstract string Name { get; }
15         public abstract Type DeclaringType { get; }
16         public abstract Type ReflectedType { get; }
17
18         public virtual Module Module
19         {
20             get
21             {
22                 // This check is necessary because for some reason, Type adds a new "Module" property that hides the inherited one instead 
23                 // of overriding.
24
25                 Type type = this as Type;
26                 if (type != null)
27                     return type.Module;
28
29                 throw NotImplemented.ByDesign;
30             }
31         }
32
33         public virtual bool HasSameMetadataDefinitionAs(MemberInfo other) { throw NotImplemented.ByDesign; }
34
35         public abstract bool IsDefined(Type attributeType, bool inherit);
36         public abstract object[] GetCustomAttributes(bool inherit);
37         public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
38
39         public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
40         public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
41         public virtual bool IsCollectible => true;
42         public virtual int MetadataToken { get { throw new InvalidOperationException(); } }
43
44         public override bool Equals(object obj) => base.Equals(obj);
45         public override int GetHashCode() => base.GetHashCode();
46
47         public static bool operator ==(MemberInfo left, MemberInfo right)
48         {
49             if (object.ReferenceEquals(left, right))
50                 return true;
51
52             if ((object)left == null || (object)right == null)
53                 return false;
54
55             Type type1, type2;
56             MethodBase method1, method2;
57             FieldInfo field1, field2;
58             EventInfo event1, event2;
59             PropertyInfo property1, property2;
60
61             if ((type1 = left as Type) != null && (type2 = right as Type) != null)
62                 return type1 == type2;
63             else if ((method1 = left as MethodBase) != null && (method2 = right as MethodBase) != null)
64                 return method1 == method2;
65             else if ((field1 = left as FieldInfo) != null && (field2 = right as FieldInfo) != null)
66                 return field1 == field2;
67             else if ((event1 = left as EventInfo) != null && (event2 = right as EventInfo) != null)
68                 return event1 == event2;
69             else if ((property1 = left as PropertyInfo) != null && (property2 = right as PropertyInfo) != null)
70                 return property1 == property2;
71
72             return false;
73         }
74
75         public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right);
76     }
77 }