Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / typeinfo.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*=============================================================================
7 **
8 ** Class: TypeInfo
9 **
10 ** <OWNER>Microsoft</OWNER>
11 **
12 **
13 ** Purpose: Notion of a type definition
14 **
15 **
16 =============================================================================*/
17
18 namespace System.Reflection
19 {
20     using System;
21     using System.Runtime.CompilerServices;
22     using System.Collections.Generic;
23     using System.Diagnostics.Contracts;
24
25     //all today's runtime Type derivations derive now from TypeInfo
26     //we make TypeInfo implement IRCT - simplifies work
27     [System.Runtime.InteropServices.ComVisible(true)]
28     [Serializable]
29     public abstract class TypeInfo:Type,IReflectableType
30     {
31         [FriendAccessAllowed]
32         internal TypeInfo() { }
33
34         TypeInfo IReflectableType.GetTypeInfo(){
35             return this;
36         }
37         public virtual Type AsType(){
38             return (Type)this;
39         }
40
41         public virtual Type[] GenericTypeParameters{
42             get{
43                 if(IsGenericTypeDefinition){
44                     return GetGenericArguments();
45                 }
46                 else{
47                     return Type.EmptyTypes;
48                 }
49
50             }
51         }
52         //a re-implementation of ISAF from Type, skipping the use of UnderlyingType
53         [Pure]
54         public virtual bool IsAssignableFrom(TypeInfo typeInfo)
55         {
56             if (typeInfo == null)
57                 return false;
58
59             if (this == typeInfo)
60                 return true;
61
62             // If c is a subclass of this class, then c can be cast to this type.
63             if (typeInfo.IsSubclassOf(this))
64                 return true;
65
66             if (this.IsInterface)
67             {
68                 return typeInfo.ImplementInterface(this);
69             }
70             else if (IsGenericParameter)
71             {
72                 Type[] constraints = GetGenericParameterConstraints();
73                 for (int i = 0; i < constraints.Length; i++)
74                     if (!constraints[i].IsAssignableFrom(typeInfo))
75                         return false;
76
77                 return true;
78             }
79
80             return false;
81         }
82 #region moved over from Type
83    // Fields
84
85         public virtual EventInfo GetDeclaredEvent(String name)
86         {
87             return GetEvent(name, Type.DeclaredOnlyLookup);
88         }
89         public virtual FieldInfo GetDeclaredField(String name)
90         {
91             return GetField(name, Type.DeclaredOnlyLookup);
92         }
93         public virtual MethodInfo GetDeclaredMethod(String name)
94         {
95             return GetMethod(name, Type.DeclaredOnlyLookup);
96         }
97
98         public virtual IEnumerable<MethodInfo> GetDeclaredMethods(String name)
99         {
100             foreach (MethodInfo method in GetMethods(Type.DeclaredOnlyLookup))
101             {
102                 if (method.Name == name)
103                     yield return method;
104             }
105         }
106         public virtual System.Reflection.TypeInfo GetDeclaredNestedType(String name)
107         {
108             var nt=GetNestedType(name, Type.DeclaredOnlyLookup);
109             if(nt == null){
110                 return null; //the extension method GetTypeInfo throws for null
111             }else{
112                 return nt.GetTypeInfo();
113             }
114         }
115         public virtual PropertyInfo GetDeclaredProperty(String name)
116         {
117             return GetProperty(name, Type.DeclaredOnlyLookup);
118         }
119
120
121
122
123
124     // Properties
125
126         public virtual IEnumerable<ConstructorInfo> DeclaredConstructors
127         {
128             get
129             {
130                 return GetConstructors(Type.DeclaredOnlyLookup);
131             }
132         }
133
134         public virtual IEnumerable<EventInfo> DeclaredEvents
135         {
136             get
137             {
138                 return GetEvents(Type.DeclaredOnlyLookup);
139             }
140         }
141
142         public virtual IEnumerable<FieldInfo> DeclaredFields
143         {
144             get
145             {
146                 return GetFields(Type.DeclaredOnlyLookup);
147             }
148         }
149
150         public virtual IEnumerable<MemberInfo> DeclaredMembers
151         {
152             get
153             {
154                 return GetMembers(Type.DeclaredOnlyLookup);
155             }
156         }
157
158         public virtual IEnumerable<MethodInfo> DeclaredMethods
159         {
160             get
161             {
162                 return GetMethods(Type.DeclaredOnlyLookup);
163             }
164         }
165         public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
166         {
167             get
168             {
169                 foreach (var t in GetNestedTypes(Type.DeclaredOnlyLookup)){
170                                 yield return t.GetTypeInfo();
171                     }
172             }
173         }
174
175         public virtual IEnumerable<PropertyInfo> DeclaredProperties
176         {
177             get
178             {
179                 return GetProperties(Type.DeclaredOnlyLookup);
180             }
181         }
182
183
184         public virtual IEnumerable<Type> ImplementedInterfaces
185         {
186             get
187             {
188                 return GetInterfaces();
189             }
190         }
191
192  
193 #endregion        
194
195     }
196 }
197