Skip to content

Commit

Permalink
Completed reflection support
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Mar 6, 2017
1 parent ee48622 commit f30cbcc
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 21 deletions.
15 changes: 15 additions & 0 deletions ILRuntime/CLR/Method/ILMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ILMethod : IMethod
Dictionary<int, int[]> jumptables;
bool isDelegateInvoke;
ILRuntimeMethodInfo refletionMethodInfo;
ILRuntimeConstructorInfo reflectionCtorInfo;
int paramCnt, localVarCnt;
Mono.Collections.Generic.Collection<Mono.Cecil.Cil.VariableDefinition> variables;
int hashCode = -1;
Expand All @@ -44,12 +45,26 @@ public MethodInfo ReflectionMethodInfo
{
get
{
if (IsConstructor)
throw new NotSupportedException();
if (refletionMethodInfo == null)
refletionMethodInfo = new ILRuntimeMethodInfo(this);
return refletionMethodInfo;
}
}

public ConstructorInfo ReflectionConstructorInfo
{
get
{
if (!IsConstructor)
throw new NotSupportedException();
if (reflectionCtorInfo == null)
reflectionCtorInfo = new ILRuntimeConstructorInfo(this);
return reflectionCtorInfo;
}
}

internal ExceptionHandler[] ExceptionHandler
{
get
Expand Down
9 changes: 8 additions & 1 deletion ILRuntime/CLR/TypeSystem/ILType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public IType FirstCLRBaseType
}
}

IType FirstCLRInterface
public IType FirstCLRInterface
{
get
{
Expand Down Expand Up @@ -663,6 +663,13 @@ ILMethod CheckGenericParams(ILMethod i, List<IType> param, ref bool match)
return genericMethod;
}

public List<ILMethod> GetConstructors()
{
if (constructors == null)
InitializeMethods();
return constructors;
}

public IMethod GetConstructor(int paramCnt)
{
if (constructors == null)
Expand Down
107 changes: 107 additions & 0 deletions ILRuntime/Reflection/ILRuntimeConstructorInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;

using ILRuntime.CLR.Method;
using ILRuntime.CLR.TypeSystem;

namespace ILRuntime.Reflection
{
public class ILRuntimeConstructorInfo : ConstructorInfo
{
ILMethod method;
ILRuntimeParameterInfo[] parameters;
public ILRuntimeConstructorInfo(ILMethod m)
{
method = m;
parameters = new ILRuntimeParameterInfo[m.ParameterCount];
for(int i = 0; i < m.ParameterCount; i++)
{
parameters[i] = new ILRuntimeParameterInfo(m.Parameters[i]);
}
}

internal ILMethod ILMethod { get { return method; } }
public override MethodAttributes Attributes
{
get
{
return MethodAttributes.Public;
}
}

public override Type DeclaringType
{
get
{
return method.DeclearingType.ReflectionType;
}
}

public override RuntimeMethodHandle MethodHandle
{
get
{
throw new NotImplementedException();
}
}

public override string Name
{
get
{
return method.Name;
}
}

public override Type ReflectedType
{
get
{
return method.DeclearingType.ReflectionType;
}
}

public override object[] GetCustomAttributes(bool inherit)
{
throw new NotImplementedException();
}

public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
throw new NotImplementedException();
}

public override MethodImplAttributes GetMethodImplementationFlags()
{
throw new NotImplementedException();
}

public override ParameterInfo[] GetParameters()
{
return parameters;
}

public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
var res = ((ILType)method.DeclearingType).Instantiate(false);
method.DeclearingType.AppDomain.Invoke(method, res, parameters);
return res;
}

public override bool IsDefined(Type attributeType, bool inherit)
{
throw new NotImplementedException();
}

public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
var res = ((ILType)method.DeclearingType).Instantiate(false);
method.DeclearingType.AppDomain.Invoke(method, res, parameters);
return res;
}
}
}
9 changes: 8 additions & 1 deletion ILRuntime/Reflection/ILRuntimeFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ public override object GetValue(object obj)

public override bool IsDefined(Type attributeType, bool inherit)
{
throw new NotImplementedException();
if (customAttributes == null)
InitializeCustomAttribute();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i] == attributeType)
return true;
}
return false;
}

public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
Expand Down
63 changes: 58 additions & 5 deletions ILRuntime/Reflection/ILRuntimeMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,44 @@ namespace ILRuntime.Reflection
public class ILRuntimeMethodInfo : MethodInfo
{
ILMethod method;
ILRuntimeParameterInfo[] parameters;
Mono.Cecil.MethodDefinition definition;
ILRuntime.Runtime.Enviorment.AppDomain appdomain;

object[] customAttributes;
Type[] attributeTypes;
public ILRuntimeMethodInfo(ILMethod m)
{
method = m;
definition = m.Definition;
appdomain = m.DeclearingType.AppDomain;
parameters = new ILRuntimeParameterInfo[m.ParameterCount];
for (int i = 0; i < m.ParameterCount; i++)
{
parameters[i] = new ILRuntimeParameterInfo(m.Parameters[i]);
}
}

void InitializeCustomAttribute()
{
customAttributes = new object[definition.CustomAttributes.Count];
attributeTypes = new Type[customAttributes.Length];
for (int i = 0; i < definition.CustomAttributes.Count; i++)
{
var attribute = definition.CustomAttributes[i];
var at = appdomain.GetType(attribute.AttributeType, null, null);
try
{
object ins = attribute.CreateInstance(at, appdomain);

attributeTypes[i] = at.ReflectionType;
customAttributes[i] = ins;
}
catch
{
attributeTypes[i] = typeof(Attribute);
}
}
}

internal ILMethod ILMethod { get { return method; } }
Expand Down Expand Up @@ -73,12 +108,23 @@ public override MethodInfo GetBaseDefinition()

public override object[] GetCustomAttributes(bool inherit)
{
throw new NotImplementedException();
if (customAttributes == null)
InitializeCustomAttribute();

return customAttributes;
}

public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
throw new NotImplementedException();
if (customAttributes == null)
InitializeCustomAttribute();
List<object> res = new List<object>();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i] == attributeType)
res.Add(customAttributes[i]);
}
return res.ToArray();
}

public override MethodImplAttributes GetMethodImplementationFlags()
Expand All @@ -95,16 +141,23 @@ public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder
{
if (method.HasThis)
{
var res = method.DeclearingType.AppDomain.Invoke(method, obj, parameters);
var res = appdomain.Invoke(method, obj, parameters);
return res;
}
else
return method.DeclearingType.AppDomain.Invoke(method, null, parameters);
return appdomain.Invoke(method, null, parameters);
}

public override bool IsDefined(Type attributeType, bool inherit)
{
throw new NotImplementedException();
if (customAttributes == null)
InitializeCustomAttribute();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i] == attributeType)
return true;
}
return false;
}
}
}
28 changes: 28 additions & 0 deletions ILRuntime/Reflection/ILRuntimeParameterInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;

using ILRuntime.CLR.TypeSystem;

namespace ILRuntime.Reflection
{
public class ILRuntimeParameterInfo : ParameterInfo
{
IType type;

public ILRuntimeParameterInfo(IType type)
{
this.type = type;
}
public override Type ParameterType
{
get
{
return type.ReflectionType;
}
}
}
}
Loading

0 comments on commit f30cbcc

Please sign in to comment.