Added MemberInfo.ReflectedType.

This commit is contained in:
jfrijters 2012-04-10 09:07:54 +00:00
Родитель 4893d02da9
Коммит fc67119ad3
8 изменённых файлов: 793 добавлений и 69 удалений

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

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009 Jeroen Frijters Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -23,6 +23,7 @@
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
namespace IKVM.Reflection namespace IKVM.Reflection
{ {
@ -58,7 +59,7 @@ namespace IKVM.Reflection
get { return GetMethodInfo().__MethodRVA; } get { return GetMethodInfo().__MethodRVA; }
} }
public override bool ContainsGenericParameters public sealed override bool ContainsGenericParameters
{ {
get { return GetMethodInfo().ContainsGenericParameters; } get { return GetMethodInfo().ContainsGenericParameters; }
} }
@ -78,66 +79,9 @@ namespace IKVM.Reflection
return parameters; return parameters;
} }
private sealed class ParameterInfoWrapper : ParameterInfo internal sealed override MemberInfo SetReflectedType(Type type)
{ {
private readonly ConstructorInfo ctor; return new ConstructorInfoWithReflectedType(type, this);
private readonly ParameterInfo forward;
internal ParameterInfoWrapper(ConstructorInfo ctor, ParameterInfo forward)
{
this.ctor = ctor;
this.forward = forward;
}
public override string Name
{
get { return forward.Name; }
}
public override Type ParameterType
{
get { return forward.ParameterType; }
}
public override ParameterAttributes Attributes
{
get { return forward.Attributes; }
}
public override int Position
{
get { return forward.Position; }
}
public override object RawDefaultValue
{
get { return forward.RawDefaultValue; }
}
public override CustomModifiers __GetCustomModifiers()
{
return forward.__GetCustomModifiers();
}
public override MemberInfo Member
{
get { return ctor; }
}
public override int MetadataToken
{
get { return forward.MetadataToken; }
}
internal override Module Module
{
get { return ctor.Module; }
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return forward.GetCustomAttributesData(attributeType);
}
} }
} }
@ -241,4 +185,115 @@ namespace IKVM.Reflection
return method.ImportTo(module); return method.ImportTo(module);
} }
} }
sealed class ConstructorInfoWithReflectedType : ConstructorInfo
{
private readonly Type reflectedType;
private readonly ConstructorInfo ctor;
internal ConstructorInfoWithReflectedType(Type reflectedType, ConstructorInfo ctor)
{
Debug.Assert(reflectedType != ctor.DeclaringType);
this.reflectedType = reflectedType;
this.ctor = ctor;
}
public override bool Equals(object obj)
{
ConstructorInfoWithReflectedType other = obj as ConstructorInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.ctor == ctor;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ ctor.GetHashCode();
}
public override MethodBody GetMethodBody()
{
return ctor.GetMethodBody();
}
public override CallingConventions CallingConvention
{
get { return ctor.CallingConvention; }
}
public override MethodAttributes Attributes
{
get { return ctor.Attributes; }
}
public override MethodImplAttributes GetMethodImplementationFlags()
{
return ctor.GetMethodImplementationFlags();
}
internal override int ParameterCount
{
get { return ctor.ParameterCount; }
}
public override Type DeclaringType
{
get { return ctor.DeclaringType; }
}
public override Type ReflectedType
{
get { return reflectedType; }
}
public override string Name
{
get { return ctor.Name; }
}
public override string ToString()
{
return ctor.ToString();
}
public override Module Module
{
get { return ctor.Module; }
}
public override int MetadataToken
{
get { return ctor.MetadataToken; }
}
public override bool __IsMissing
{
get { return ctor.__IsMissing; }
}
internal override MethodInfo GetMethodInfo()
{
return ctor.GetMethodInfo();
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return ctor.GetCustomAttributesData(attributeType);
}
internal override MethodInfo GetMethodOnTypeDefinition()
{
return ctor.GetMethodOnTypeDefinition();
}
internal override MethodSignature MethodSignature
{
get { return ctor.MethodSignature; }
}
internal override int ImportTo(Emit.ModuleBuilder module)
{
return ctor.ImportTo(module);
}
}
} }

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

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009 Jeroen Frijters Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -21,6 +21,8 @@
jeroen@frijters.net jeroen@frijters.net
*/ */
using System.Collections.Generic;
using System.Diagnostics;
namespace IKVM.Reflection namespace IKVM.Reflection
{ {
@ -94,5 +96,141 @@ namespace IKVM.Reflection
&& BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic) && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
&& BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance); && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
} }
internal sealed override MemberInfo SetReflectedType(Type type)
{
return new EventInfoWithReflectedType(type, this);
}
}
sealed class EventInfoWithReflectedType : EventInfo
{
private readonly Type reflectedType;
private readonly EventInfo eventInfo;
internal EventInfoWithReflectedType(Type reflectedType, EventInfo eventInfo)
{
Debug.Assert(reflectedType != eventInfo.DeclaringType);
this.reflectedType = reflectedType;
this.eventInfo = eventInfo;
}
public override EventAttributes Attributes
{
get { return eventInfo.Attributes; }
}
public override MethodInfo GetAddMethod(bool nonPublic)
{
return (MethodInfo)eventInfo.GetAddMethod(nonPublic).SetReflectedType(reflectedType);
}
public override MethodInfo GetRaiseMethod(bool nonPublic)
{
return (MethodInfo)eventInfo.GetRaiseMethod(nonPublic).SetReflectedType(reflectedType);
}
public override MethodInfo GetRemoveMethod(bool nonPublic)
{
return (MethodInfo)eventInfo.GetRemoveMethod(nonPublic).SetReflectedType(reflectedType);
}
public override MethodInfo[] GetOtherMethods(bool nonPublic)
{
MethodInfo[] methods = eventInfo.GetOtherMethods(nonPublic);
for (int i = 0; i < methods.Length; i++)
{
methods[i] = (MethodInfo)methods[i].SetReflectedType(reflectedType);
}
return methods;
}
public override MethodInfo[] __GetMethods()
{
MethodInfo[] methods = eventInfo.__GetMethods();
for (int i = 0; i < methods.Length; i++)
{
methods[i] = (MethodInfo)methods[i].SetReflectedType(reflectedType);
}
return methods;
}
public override Type EventHandlerType
{
get { return eventInfo.EventHandlerType; }
}
internal override bool IsPublic
{
get { return eventInfo.IsPublic; }
}
internal override bool IsNonPrivate
{
get { return eventInfo.IsNonPrivate; }
}
internal override bool IsStatic
{
get { return eventInfo.IsStatic; }
}
internal override EventInfo BindTypeParameters(Type type)
{
return eventInfo.BindTypeParameters(type);
}
public override string ToString()
{
return eventInfo.ToString();
}
public override bool __IsMissing
{
get { return eventInfo.__IsMissing; }
}
public override Type DeclaringType
{
get { return eventInfo.DeclaringType; }
}
public override Type ReflectedType
{
get { return reflectedType; }
}
public override bool Equals(object obj)
{
EventInfoWithReflectedType other = obj as EventInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.eventInfo == eventInfo;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ eventInfo.GetHashCode();
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return eventInfo.GetCustomAttributesData(attributeType);
}
public override int MetadataToken
{
get { return eventInfo.MetadataToken; }
}
public override Module Module
{
get { return eventInfo.Module; }
}
public override string Name
{
get { return eventInfo.Name; }
}
} }
} }

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

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009 Jeroen Frijters Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -22,6 +22,8 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace IKVM.Reflection namespace IKVM.Reflection
{ {
@ -147,5 +149,116 @@ namespace IKVM.Reflection
&& BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic) && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
&& BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance); && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
} }
internal sealed override MemberInfo SetReflectedType(Type type)
{
return new FieldInfoWithReflectedType(type, this);
}
}
sealed class FieldInfoWithReflectedType : FieldInfo
{
private readonly Type reflectedType;
private readonly FieldInfo field;
internal FieldInfoWithReflectedType(Type reflectedType, FieldInfo field)
{
Debug.Assert(reflectedType != field.DeclaringType);
this.reflectedType = reflectedType;
this.field = field;
}
public override FieldAttributes Attributes
{
get { return field.Attributes; }
}
public override void __GetDataFromRVA(byte[] data, int offset, int length)
{
field.__GetDataFromRVA(data, offset, length);
}
public override int __FieldRVA
{
get { return field.__FieldRVA; }
}
public override Object GetRawConstantValue()
{
return field.GetRawConstantValue();
}
internal override FieldSignature FieldSignature
{
get { return field.FieldSignature; }
}
public override FieldInfo __GetFieldOnTypeDefinition()
{
return field.__GetFieldOnTypeDefinition();
}
internal override int ImportTo(Emit.ModuleBuilder module)
{
return field.ImportTo(module);
}
internal override FieldInfo BindTypeParameters(Type type)
{
return field.BindTypeParameters(type);
}
public override bool __IsMissing
{
get { return field.__IsMissing; }
}
public override Type DeclaringType
{
get { return field.DeclaringType; }
}
public override Type ReflectedType
{
get { return reflectedType; }
}
public override bool Equals(object obj)
{
FieldInfoWithReflectedType other = obj as FieldInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.field == field;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ field.GetHashCode();
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return field.GetCustomAttributesData(attributeType);
}
public override int MetadataToken
{
get { return field.MetadataToken; }
}
public override Module Module
{
get { return field.Module; }
}
public override string Name
{
get { return field.Name; }
}
public override string ToString()
{
return field.ToString();
}
} }
} }

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

@ -42,6 +42,13 @@ namespace IKVM.Reflection
public abstract Type DeclaringType { get; } public abstract Type DeclaringType { get; }
public abstract MemberTypes MemberType { get; } public abstract MemberTypes MemberType { get; }
public virtual Type ReflectedType
{
get { return DeclaringType; }
}
internal abstract MemberInfo SetReflectedType(Type type);
public virtual int MetadataToken public virtual int MetadataToken
{ {
get { throw new NotSupportedException(); } get { throw new NotSupportedException(); }

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

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009 Jeroen Frijters Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -22,6 +22,8 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text; using System.Text;
namespace IKVM.Reflection namespace IKVM.Reflection
@ -152,5 +154,201 @@ namespace IKVM.Reflection
{ {
get { return !IsStatic; } get { return !IsStatic; }
} }
internal sealed override MemberInfo SetReflectedType(Type type)
{
return new MethodInfoWithReflectedType(type, this);
}
}
sealed class MethodInfoWithReflectedType : MethodInfo
{
private readonly Type reflectedType;
private readonly MethodInfo method;
internal MethodInfoWithReflectedType(Type reflectedType, MethodInfo method)
{
Debug.Assert(reflectedType != method.DeclaringType);
this.reflectedType = reflectedType;
this.method = method;
}
public override bool Equals(object obj)
{
MethodInfoWithReflectedType other = obj as MethodInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.method == method;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ method.GetHashCode();
}
internal override MethodSignature MethodSignature
{
get { return method.MethodSignature; }
}
internal override int ParameterCount
{
get { return method.ParameterCount; }
}
public override ParameterInfo[] GetParameters()
{
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
}
return parameters;
}
public override MethodAttributes Attributes
{
get { return method.Attributes; }
}
public override MethodImplAttributes GetMethodImplementationFlags()
{
return method.GetMethodImplementationFlags();
}
public override MethodBody GetMethodBody()
{
return method.GetMethodBody();
}
public override CallingConventions CallingConvention
{
get { return method.CallingConvention; }
}
public override int __MethodRVA
{
get { return method.__MethodRVA; }
}
public override Type ReturnType
{
get { return method.ReturnType; }
}
public override ParameterInfo ReturnParameter
{
get { return new ParameterInfoWrapper(this, method.ReturnParameter); }
}
public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
{
return (MethodInfo)method.MakeGenericMethod(typeArguments).SetReflectedType(reflectedType);
}
public override MethodInfo GetGenericMethodDefinition()
{
return (MethodInfo)method.GetGenericMethodDefinition().SetReflectedType(reflectedType);
}
public override string ToString()
{
return method.ToString();
}
public override MethodInfo[] __GetMethodImpls()
{
return method.__GetMethodImpls();
}
internal override Type GetGenericMethodArgument(int index)
{
return method.GetGenericMethodArgument(index);
}
internal override int GetGenericMethodArgumentCount()
{
return method.GetGenericMethodArgumentCount();
}
internal override MethodInfo GetMethodOnTypeDefinition()
{
return method.GetMethodOnTypeDefinition();
}
internal override bool HasThis
{
get { return method.HasThis; }
}
public override Module Module
{
get { return method.Module; }
}
public override Type DeclaringType
{
get { return method.DeclaringType; }
}
public override Type ReflectedType
{
get { return reflectedType; }
}
public override string Name
{
get { return method.Name; }
}
internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
{
return method.ImportTo(module);
}
public override MethodBase __GetMethodOnTypeDefinition()
{
return method.__GetMethodOnTypeDefinition();
}
public override bool __IsMissing
{
get { return method.__IsMissing; }
}
internal override MethodBase BindTypeParameters(Type type)
{
return method.BindTypeParameters(type);
}
public override bool ContainsGenericParameters
{
get { return method.ContainsGenericParameters; }
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return method.GetCustomAttributesData(attributeType);
}
public override Type[] GetGenericArguments()
{
return method.GetGenericArguments();
}
public override bool IsGenericMethod
{
get { return method.IsGenericMethod; }
}
public override bool IsGenericMethodDefinition
{
get { return method.IsGenericMethodDefinition; }
}
public override int MetadataToken
{
get { return method.MetadataToken; }
}
} }
} }

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

@ -113,4 +113,66 @@ namespace IKVM.Reflection
return this.Module.GetCustomAttributes(this.MetadataToken, attributeType); return this.Module.GetCustomAttributes(this.MetadataToken, attributeType);
} }
} }
sealed class ParameterInfoWrapper : ParameterInfo
{
private readonly MemberInfo member;
private readonly ParameterInfo forward;
internal ParameterInfoWrapper(MemberInfo member, ParameterInfo forward)
{
this.member = member;
this.forward = forward;
}
public override string Name
{
get { return forward.Name; }
}
public override Type ParameterType
{
get { return forward.ParameterType; }
}
public override ParameterAttributes Attributes
{
get { return forward.Attributes; }
}
public override int Position
{
get { return forward.Position; }
}
public override object RawDefaultValue
{
get { return forward.RawDefaultValue; }
}
public override CustomModifiers __GetCustomModifiers()
{
return forward.__GetCustomModifiers();
}
public override MemberInfo Member
{
get { return member; }
}
public override int MetadataToken
{
get { return forward.MetadataToken; }
}
internal override Module Module
{
get { return member.Module; }
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return forward.GetCustomAttributesData(attributeType);
}
}
} }

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

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009 Jeroen Frijters Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -22,6 +22,7 @@
*/ */
using System; using System;
using System.Collections.Generic;
namespace IKVM.Reflection namespace IKVM.Reflection
{ {
@ -106,7 +107,7 @@ namespace IKVM.Reflection
} }
} }
public ParameterInfo[] GetIndexParameters() public virtual ParameterInfo[] GetIndexParameters()
{ {
ParameterInfo[] parameters = new ParameterInfo[this.PropertySignature.ParameterCount]; ParameterInfo[] parameters = new ParameterInfo[this.PropertySignature.ParameterCount];
for (int i = 0; i < parameters.Length; i++) for (int i = 0; i < parameters.Length; i++)
@ -183,5 +184,150 @@ namespace IKVM.Reflection
&& BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic) && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
&& BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance); && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
} }
internal sealed override MemberInfo SetReflectedType(Type type)
{
return new PropertyInfoWithReflectedType(type, this);
}
}
sealed class PropertyInfoWithReflectedType : PropertyInfo
{
private readonly Type reflectedType;
private readonly PropertyInfo property;
internal PropertyInfoWithReflectedType(Type reflectedType, PropertyInfo property)
{
this.reflectedType = reflectedType;
this.property = property;
}
public override PropertyAttributes Attributes
{
get { return property.Attributes; }
}
public override bool CanRead
{
get { return property.CanRead; }
}
public override bool CanWrite
{
get { return property.CanWrite; }
}
public override MethodInfo GetGetMethod(bool nonPublic)
{
return (MethodInfo)property.GetGetMethod(nonPublic).SetReflectedType(reflectedType);
}
public override MethodInfo GetSetMethod(bool nonPublic)
{
return (MethodInfo)property.GetSetMethod(nonPublic).SetReflectedType(reflectedType);
}
public override MethodInfo[] GetAccessors(bool nonPublic)
{
MethodInfo[] methods = GetAccessors(nonPublic);
for (int i = 0; i < methods.Length; i++)
{
methods[i] = (MethodInfo)methods[i].SetReflectedType(reflectedType);
}
return methods;
}
public override object GetRawConstantValue()
{
return property.GetRawConstantValue();
}
internal override bool IsPublic
{
get { return property.IsPublic; }
}
internal override bool IsNonPrivate
{
get { return property.IsNonPrivate; }
}
internal override bool IsStatic
{
get { return property.IsStatic; }
}
internal override PropertySignature PropertySignature
{
get { return property.PropertySignature; }
}
public override ParameterInfo[] GetIndexParameters()
{
ParameterInfo[] parameters = property.GetIndexParameters();
for (int i = 0; i < parameters.Length; i++)
{
parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
}
return parameters;
}
internal override PropertyInfo BindTypeParameters(Type type)
{
return property.BindTypeParameters(type);
}
public override string ToString()
{
return property.ToString();
}
public override bool __IsMissing
{
get { return property.__IsMissing; }
}
public override Type DeclaringType
{
get { return property.DeclaringType; }
}
public override Type ReflectedType
{
get { return reflectedType; }
}
public override bool Equals(object obj)
{
PropertyInfoWithReflectedType other = obj as PropertyInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.property == property;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ property.GetHashCode();
}
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return property.GetCustomAttributesData(attributeType);
}
public override int MetadataToken
{
get { return property.MetadataToken; }
}
public override Module Module
{
get { return property.Module; }
}
public override string Name
{
get { return property.Name; }
}
} }
} }

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

@ -624,7 +624,7 @@ namespace IKVM.Reflection
{ {
if (member is T && member.BindingFlagsMatchInherited(flags)) if (member is T && member.BindingFlagsMatchInherited(flags))
{ {
list.Add((T)member); list.Add((T)member.SetReflectedType(this));
} }
} }
} }
@ -687,7 +687,7 @@ namespace IKVM.Reflection
} }
throw new AmbiguousMatchException(); throw new AmbiguousMatchException();
} }
found = (T)member; found = (T)member.SetReflectedType(this);
} }
} }
} }
@ -808,7 +808,7 @@ namespace IKVM.Reflection
} }
baseMethods.Add(mi.GetBaseDefinition()); baseMethods.Add(mi.GetBaseDefinition());
} }
list.Add(mi); list.Add((MethodInfo)mi.SetReflectedType(this));
} }
} }
} }
@ -1857,6 +1857,11 @@ namespace IKVM.Reflection
{ {
return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic); return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
} }
internal sealed override MemberInfo SetReflectedType(Type type)
{
throw new InvalidOperationException();
}
} }
abstract class ElementHolderType : Type abstract class ElementHolderType : Type