Added TypeInfo (from .NET 4.5).

This commit is contained in:
jfrijters 2012-09-05 08:58:51 +00:00
Родитель 48c59aa20c
Коммит 46816a2e7e
8 изменённых файлов: 185 добавлений и 9 удалений

Просмотреть файл

@ -27,7 +27,7 @@ using System.Text;
namespace IKVM.Reflection.Emit
{
public sealed class EnumBuilder : Type
public sealed class EnumBuilder : TypeInfo
{
private readonly TypeBuilder typeBuilder;
private readonly FieldBuilder fieldBuilder;

Просмотреть файл

@ -31,7 +31,7 @@ using IKVM.Reflection.Writer;
namespace IKVM.Reflection.Emit
{
public sealed class GenericTypeParameterBuilder : Type
public sealed class GenericTypeParameterBuilder : TypeInfo
{
private readonly string name;
private readonly TypeBuilder type;
@ -246,7 +246,7 @@ namespace IKVM.Reflection.Emit
}
}
public sealed class TypeBuilder : Type, ITypeOwner
public sealed class TypeBuilder : TypeInfo, ITypeOwner
{
public const int UnspecifiedTypeSize = 0;
private readonly ITypeOwner owner;
@ -1101,7 +1101,7 @@ namespace IKVM.Reflection.Emit
}
}
sealed class BakedType : Type
sealed class BakedType : TypeInfo
{
internal BakedType(TypeBuilder typeBuilder)
: base(typeBuilder)

Просмотреть файл

@ -116,6 +116,7 @@
<Compile Include="StrongNameKeyPair.cs" />
<Compile Include="Type.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeInfo.cs" />
<Compile Include="TypeNameParser.cs" />
<Compile Include="Universe.cs" />
<Compile Include="Util.cs" />

Просмотреть файл

@ -28,7 +28,7 @@ using IKVM.Reflection.Metadata;
namespace IKVM.Reflection.Reader
{
abstract class TypeParameterType : Type
abstract class TypeParameterType : TypeInfo
{
public sealed override string AssemblyQualifiedName
{

Просмотреть файл

@ -29,7 +29,7 @@ using IKVM.Reflection.Metadata;
namespace IKVM.Reflection.Reader
{
sealed class TypeDefImpl : Type
sealed class TypeDefImpl : TypeInfo
{
private readonly ModuleReader module;
private readonly int index;

Просмотреть файл

@ -1946,9 +1946,20 @@ namespace IKVM.Reflection
// types don't have pseudo custom attributes
return null;
}
// in .NET this is an extension method, but we target .NET 2.0, so we have an instance method
public TypeInfo GetTypeInfo()
{
TypeInfo type = this as TypeInfo;
if (type == null)
{
throw new MissingMemberException(this);
}
return type;
}
}
abstract class ElementHolderType : Type
abstract class ElementHolderType : TypeInfo
{
protected readonly Type elementType;
private int token;
@ -2488,7 +2499,7 @@ namespace IKVM.Reflection
}
}
sealed class GenericTypeInstance : Type
sealed class GenericTypeInstance : TypeInfo
{
private readonly Type type;
private readonly Type[] args;
@ -2851,7 +2862,7 @@ namespace IKVM.Reflection
}
}
sealed class FunctionPointerType : Type
sealed class FunctionPointerType : TypeInfo
{
private readonly Universe universe;
private readonly __StandAloneMethodSig sig;
@ -2975,5 +2986,10 @@ namespace IKVM.Reflection
{
get { throw new InvalidOperationException(); }
}
public override bool __IsMissing
{
get { throw new InvalidOperationException(); }
}
}
}

158
reflect/TypeInfo.cs Normal file
Просмотреть файл

@ -0,0 +1,158 @@
/*
Copyright (C) 2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
namespace IKVM.Reflection
{
public interface IReflectableType
{
TypeInfo GetTypeInfo();
}
public static class IntrospectionExtensions
{
// we target .NET 2.0 so we can't define an extension method
public static TypeInfo GetTypeInfo(/*this*/ Type type)
{
return type.GetTypeInfo();
}
}
public abstract class TypeInfo : Type, IReflectableType
{
private const BindingFlags Flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
internal TypeInfo()
{
}
internal TypeInfo(Type underlyingType)
: base(underlyingType)
{
}
public IEnumerable<ConstructorInfo> DeclaredConstructors
{
get { return GetConstructors(Flags); }
}
public IEnumerable<EventInfo> DeclaredEvents
{
get { return GetEvents(Flags); }
}
public IEnumerable<FieldInfo> DeclaredFields
{
get { return GetFields(Flags); }
}
public IEnumerable<MemberInfo> DeclaredMembers
{
get { return GetMembers(Flags); }
}
public IEnumerable<MethodInfo> DeclaredMethods
{
get { return GetMethods(Flags); }
}
public IEnumerable<TypeInfo> DeclaredNestedTypes
{
get
{
Type[] types = GetNestedTypes(Flags);
TypeInfo[] typeInfos = new TypeInfo[types.Length];
for (int i = 0; i < types.Length; i++)
{
typeInfos[i] = types[i].GetTypeInfo();
}
return typeInfos;
}
}
public IEnumerable<PropertyInfo> DeclaredProperties
{
get { return GetProperties(Flags); }
}
public Type[] GenericTypeParameters
{
get { return IsGenericTypeDefinition ? GetGenericArguments() : Type.EmptyTypes; }
}
public IEnumerable<Type> ImplementedInterfaces
{
get { return __GetDeclaredInterfaces(); }
}
public Type AsType()
{
return this;
}
public EventInfo GetDeclaredEvent(string name)
{
return GetEvent(name, Flags);
}
public FieldInfo GetDeclaredField(string name)
{
return GetField(name, Flags);
}
public MethodInfo GetDeclaredMethod(string name)
{
return GetMethod(name, Flags);
}
public IEnumerable<MethodInfo> GetDeclaredMethods(string name)
{
List<MethodInfo> methods = new List<MethodInfo>();
foreach (MethodInfo method in GetMethods(Flags))
{
if (method.Name == name)
{
methods.Add(method);
}
}
return methods;
}
public TypeInfo GetDeclaredNestedType(string name)
{
return GetNestedType(name, Flags).GetTypeInfo();
}
public PropertyInfo GetDeclaredProperty(string name)
{
return GetProperty(name, Flags);
}
public bool IsAssignableFrom(TypeInfo typeInfo)
{
return base.IsAssignableFrom(typeInfo);
}
}
}

Просмотреть файл

@ -56,6 +56,7 @@
<include name="StandAloneMethodSig.cs" />
<include name="StrongNameKeyPair.cs" />
<include name="Type.cs" />
<include name="TypeInfo.cs" />
<include name="TypeNameParser.cs" />
<include name="Universe.cs" />
<include name="Util.cs" />