Skip to content

Commit

Permalink
Merge pull request #672 from Endures/ParameterInfo
Browse files Browse the repository at this point in the history
索引器重载时属性的GetMethod和SetMethod不正确
  • Loading branch information
liiir1985 authored Mar 18, 2022
2 parents 4c841de + 7ff1df3 commit 4939837
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ILRuntime/Reflection/ILRuntimeConstructorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ILRuntimeConstructorInfo(ILMethod m)
for(int i = 0; i < m.ParameterCount; i++)
{
var pd = m.Definition.Parameters[i];
parameters[i] = new ILRuntimeParameterInfo(pd, m.Parameters[i], this);
parameters[i] = new ILRuntimeParameterInfo(pd, m.Parameters[i], this, m.AppDomain);
}
}

Expand Down
7 changes: 6 additions & 1 deletion ILRuntime/Reflection/ILRuntimeMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ILRuntimeMethodInfo(ILMethod m)
for (int i = 0; i < m.ParameterCount; i++)
{
var pd = m.Definition.Parameters[i];
parameters[i] = new ILRuntimeParameterInfo(pd, m.Parameters[i], this);
parameters[i] = new ILRuntimeParameterInfo(pd, m.Parameters[i], this, appdomain);
}
}

Expand Down Expand Up @@ -250,5 +250,10 @@ public override Delegate CreateDelegate(Type delegateType, object target)
return del.Delegate;
}
#endif

public override string ToString()
{
return definition == null ? base.ToString() : definition.ToString();
}
}
}
95 changes: 85 additions & 10 deletions ILRuntime/Reflection/ILRuntimeParameterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,99 @@ namespace ILRuntime.Reflection
{
public class ILRuntimeParameterInfo : ParameterInfo
{
IType type;
MethodBase method;
public IType IType { get; private set; }
public ILRuntime.Runtime.Enviorment.AppDomain AppDomain{ get; private set; }

Mono.Cecil.ParameterDefinition definition;
Attribute[] customAttributes;
Type[] attributeTypes;

public ILRuntimeParameterInfo(Mono.Cecil.ParameterDefinition definition, IType type, MethodBase method)
public ILRuntimeParameterInfo(Mono.Cecil.ParameterDefinition definition, IType type, MemberInfo member, ILRuntime.Runtime.Enviorment.AppDomain appdomain)
{
this.type = type;
this.method = method;
this.MemberImpl = method;
this.IType = type;
this.definition = definition;
this.AppDomain = appdomain;

AttrsImpl = (ParameterAttributes)definition.Attributes;
ClassImpl = type.ReflectionType;
DefaultValueImpl = definition.Constant;
MemberImpl = member;
NameImpl = definition.Name;
PositionImpl = definition.Index;
}
public override Type ParameterType

void InitializeCustomAttribute()
{
get
customAttributes = new Attribute[definition.CustomAttributes.Count];
attributeTypes = new Type[customAttributes.Length];
for (int i = 0; i < definition.CustomAttributes.Count; i++)
{
return type.ReflectionType;
var attribute = definition.CustomAttributes[i];
var at = AppDomain.GetType(attribute.AttributeType, null, null);
try
{
Attribute ins = attribute.CreateInstance(at, AppDomain) as Attribute;

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

public override bool HasDefaultValue
{
get { return definition.HasDefault; }
}

public override object DefaultValue
{
get { return DefaultValueImpl; }
}

public override object RawDefaultValue
{
get { return DefaultValueImpl; }
}

public override int MetadataToken
{
get { return definition.MetadataToken.ToInt32(); }
}

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

return customAttributes;
}

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

public override bool IsDefined(Type attributeType, bool inherit)
{
var result = GetCustomAttributes(attributeType, inherit);
return result != null && result.Length > 0;
}

public override string ToString()
{
return definition == null ? base.ToString() : definition.ToString();
}
}
}
}
15 changes: 14 additions & 1 deletion ILRuntime/Reflection/ILRuntimePropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ILRuntimePropertyInfo : PropertyInfo
ILType dType;
Mono.Cecil.PropertyDefinition definition;
ILRuntime.Runtime.Enviorment.AppDomain appdomain;
ILRuntimeParameterInfo[] parameters;

Attribute[] customAttributes;
Type[] attributeTypes;
Expand Down Expand Up @@ -64,6 +65,13 @@ public ILRuntimePropertyInfo(Mono.Cecil.PropertyDefinition definition, ILType dT
this.definition = definition;
this.dType = dType;
appdomain = dType.AppDomain;
parameters = new ILRuntimeParameterInfo[definition.Parameters.Count];
for (int i = 0; i < definition.Parameters.Count; i++)
{
var pd = definition.Parameters[i];
var parameterType = dType.AppDomain.GetType(pd.ParameterType, null, null);
parameters[i] = new ILRuntimeParameterInfo(pd, parameterType, this, appdomain);
}
}

void InitializeCustomAttribute()
Expand Down Expand Up @@ -211,7 +219,7 @@ public override MethodInfo GetGetMethod(bool nonPublic)

public override ParameterInfo[] GetIndexParameters()
{
return new ParameterInfo[0];
return parameters;
}

public override MethodInfo GetSetMethod(bool nonPublic)
Expand Down Expand Up @@ -262,5 +270,10 @@ public override void SetValue(object obj, object value, BindingFlags invokeAttr,
else
throw new ArgumentException("Index count mismatch");
}

public override string ToString()
{
return definition == null ? base.ToString() : definition.ToString();
}
}
}
8 changes: 6 additions & 2 deletions ILRuntime/Reflection/ILRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ void InitializeProperties()
ILRuntimePropertyInfo pi = new ILRuntimePropertyInfo(pd, type);
properties[i] = pi;
if (pd.GetMethod != null)
pi.Getter = type.GetMethod(pd.GetMethod.Name, pd.GetMethod.Parameters.Count) as ILMethod;
{
pi.Getter = type.GetMethod(pd.GetMethod.Name, pd.GetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, null, null)).ToList(), null) as ILMethod;
}
if (pd.SetMethod != null)
pi.Setter = type.GetMethod(pd.SetMethod.Name, pd.SetMethod.Parameters.Count) as ILMethod;
{
pi.Setter = type.GetMethod(pd.SetMethod.Name, pd.SetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, null, null)).ToList(), null) as ILMethod;
}
}
}

Expand Down
47 changes: 47 additions & 0 deletions TestCases/ReflectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,36 @@ public static void ReflectionTest08()
throw new Exception("attr.Name != Example");
}
}

public static void TestPropertyIndexParametersInfo()
{
foreach (var pi in typeof(TestCls).GetProperties())
{
if (pi.GetIndexParameters().Length <= 0) // 只获取索引器
continue;

List<string> parametersInfo = new List<string>();
foreach (var p in pi.GetIndexParameters())
{
parametersInfo.Add(string.Format("[{0}, type:{1}, default value:{2}]", p.Name, p.ParameterType.FullName, p.DefaultValue));
}
Console.WriteLine(string.Format("Property:{0}.{1}, parameters:{2}, getter:{3}", pi.DeclaringType.FullName, pi.Name, string.Join(",", parametersInfo), pi.GetMethod.ToString()));
}
}

public static void TestMethodParametersInfo()
{
var md = typeof(TestCls).GetMethod("Do");
List<string> parametersInfo = new List<string>();
foreach (var p in md.GetParameters())
{
parametersInfo.Add(string.Format("[{0}, type:{1}, is out:{2}, default value:{3}]", p.Name, p.ParameterType.FullName, p.IsOut, p.DefaultValue));
}
var att = md.GetParameters()[0].GetCustomAttributes(true);
Console.WriteLine("Method:TestCls.Do, parameters:" + string.Join(",", parametersInfo));
Console.WriteLine("Method:TestCls.Do, first parameter has custom attribute:" + ((Attribute)att[0]).GetType().FullName);
}

[Obsolete("gasdgas")]
public class TestCls
{
Expand All @@ -179,6 +209,23 @@ public static void bar()
{
Console.WriteLine("bar");
}

[System.Runtime.CompilerServices.IndexerName("Ccc")]
public bool this[int i]
{
get { return true; }
}

[System.Runtime.CompilerServices.IndexerName("Ccc")]
public bool this[string s]
{
get { return false; }
}

public static void Do([Test] int i, out int ii, string s = "123")
{
ii = 0;
}
}


Expand Down

0 comments on commit 4939837

Please sign in to comment.