2 Copyright (C) 2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
27 namespace IKVM.Reflection
29 public interface IReflectableType
31 TypeInfo GetTypeInfo();
34 public static class IntrospectionExtensions
36 // we target .NET 2.0 so we can't define an extension method
37 public static TypeInfo GetTypeInfo(/*this*/ Type type)
39 return type.GetTypeInfo();
43 public abstract class TypeInfo : Type, IReflectableType
45 private const BindingFlags Flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
51 internal TypeInfo(Type underlyingType)
52 : base(underlyingType)
56 public IEnumerable<ConstructorInfo> DeclaredConstructors
58 get { return GetConstructors(Flags); }
61 public IEnumerable<EventInfo> DeclaredEvents
63 get { return GetEvents(Flags); }
66 public IEnumerable<FieldInfo> DeclaredFields
68 get { return GetFields(Flags); }
71 public IEnumerable<MemberInfo> DeclaredMembers
73 get { return GetMembers(Flags); }
76 public IEnumerable<MethodInfo> DeclaredMethods
78 get { return GetMethods(Flags); }
81 public IEnumerable<TypeInfo> DeclaredNestedTypes
85 Type[] types = GetNestedTypes(Flags);
86 TypeInfo[] typeInfos = new TypeInfo[types.Length];
87 for (int i = 0; i < types.Length; i++)
89 typeInfos[i] = types[i].GetTypeInfo();
95 public IEnumerable<PropertyInfo> DeclaredProperties
97 get { return GetProperties(Flags); }
100 public Type[] GenericTypeParameters
102 get { return IsGenericTypeDefinition ? GetGenericArguments() : Type.EmptyTypes; }
105 public IEnumerable<Type> ImplementedInterfaces
107 get { return __GetDeclaredInterfaces(); }
115 public EventInfo GetDeclaredEvent(string name)
117 return GetEvent(name, Flags);
120 public FieldInfo GetDeclaredField(string name)
122 return GetField(name, Flags);
125 public MethodInfo GetDeclaredMethod(string name)
127 return GetMethod(name, Flags);
130 public IEnumerable<MethodInfo> GetDeclaredMethods(string name)
132 List<MethodInfo> methods = new List<MethodInfo>();
133 foreach (MethodInfo method in GetMethods(Flags))
135 if (method.Name == name)
143 public TypeInfo GetDeclaredNestedType(string name)
145 return GetNestedType(name, Flags).GetTypeInfo();
148 public PropertyInfo GetDeclaredProperty(string name)
150 return GetProperty(name, Flags);
153 public bool IsAssignableFrom(TypeInfo typeInfo)
155 return base.IsAssignableFrom(typeInfo);