Skip to content

Commit

Permalink
Fixed Ourpalm#702
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Jul 7, 2022
1 parent 0035b81 commit f557c88
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
30 changes: 23 additions & 7 deletions ILRuntime/Reflection/ILRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,22 @@ public override FieldInfo GetField(string name, BindingFlags bindingAttr)
{
if (fields == null)
InitializeFields();
foreach(var i in fields)
bool isPublic = (bindingAttr & BindingFlags.Public) == BindingFlags.Public;
bool isPrivate = (bindingAttr & BindingFlags.NonPublic) == BindingFlags.NonPublic;
bool isStatic = (bindingAttr & BindingFlags.Static) == BindingFlags.Static;
bool isInstance = (bindingAttr & BindingFlags.Instance) == BindingFlags.Instance;
bool isDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
for (int i = 0; i < fields.Length; i++)
{
if (i.Name == name)
return i;
FieldInfo fi = fields[i];
if (isPublic != fi.IsPublic && isPrivate != !fi.IsPublic)
continue;
if ((isStatic != fi.IsStatic) && (isInstance != !fi.IsStatic))
continue;
if (isDeclaredOnly && i < type.FieldStartIndex)
continue;
if (fi.Name == name)
return fi;
}
if (BaseType != null && BaseType is ILRuntimeWrapperType)
{
Expand All @@ -323,14 +335,18 @@ public override FieldInfo[] GetFields(BindingFlags bindingAttr)
bool isPrivate = (bindingAttr & BindingFlags.NonPublic) == BindingFlags.NonPublic;
bool isStatic = (bindingAttr & BindingFlags.Static) == BindingFlags.Static;
bool isInstance = (bindingAttr & BindingFlags.Instance) == BindingFlags.Instance;
bool isDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
List<FieldInfo> res = new List<FieldInfo>();
foreach(var i in fields)
for (int i = 0; i < fields.Length; i++)
{
if (isPublic != i.IsPublic && isPrivate != !i.IsPublic)
FieldInfo fi = fields[i];
if (isPublic != fi.IsPublic && isPrivate != !fi.IsPublic)
continue;
if ((isStatic != i.IsStatic) && (isInstance != !i.IsStatic))
if ((isStatic != fi.IsStatic) && (isInstance != !fi.IsStatic))
continue;
res.Add(i);
if (isDeclaredOnly && i < type.FieldStartIndex)
continue;
res.Add(fi);
}
if ((bindingAttr & BindingFlags.DeclaredOnly) != BindingFlags.DeclaredOnly)
{
Expand Down
1 change: 1 addition & 0 deletions ILRuntimeTestBase/Adapters/helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void Init(ILRuntime.Runtime.Enviorment.AppDomain app)
app.DelegateManager.RegisterFunctionDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>();
app.DelegateManager.RegisterFunctionDelegate<ILRuntimeTest.TestFramework.TestVector3, System.Single>();
app.DelegateManager.RegisterMethodDelegate<ILRuntimeTest.TestFramework.TestVector3>();
app.DelegateManager.RegisterFunctionDelegate<System.Reflection.FieldInfo, System.String>();
// delegate convertor
app.DelegateManager.RegisterDelegateConvertor<ILRuntimeTest.TestFramework.TestValueTypeDelegate>((act) =>
{
Expand Down
22 changes: 22 additions & 0 deletions TestCases/ReflectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,5 +579,27 @@ class TestC
{
public string Name;
}

public class Base
{
public int BaseIntVal;
}

public class Impl : Base
{
public bool ImplBoolVal;
}

public static void ReflectionTest22()
{
var t = typeof(Impl);
var flag = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.SetProperty;
var fs = t.GetFields(flag).ToList();
if (fs.Count != 1)
{
throw new Exception($"Field count mismatch,detail: {string.Join(",", fs.Select(f => f.Name))}");
}
}
}
}

0 comments on commit f557c88

Please sign in to comment.