Skip to content

Commit

Permalink
Merge pull request Ourpalm#707 from tinysnake/master
Browse files Browse the repository at this point in the history
修复ILRuntimeFieldInfo无法正确读取常量的bug
  • Loading branch information
liiir1985 authored Oct 7, 2022
2 parents b8cb257 + 3a6228d commit 479793b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ILRuntime/Reflection/ILRuntimeFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public override object GetValue(object obj)
ILTypeInstance ins;
if (isStatic)
{
if (definition.HasConstant)
return definition.Constant;
ins = ilType.StaticInstance;
}
else
Expand Down
11 changes: 10 additions & 1 deletion TestCases/ReflectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ public static void ReflectionTest04()
public static void ReflectionTest05()
{
var fi = typeof(TestCls).GetField("aa", BindingFlags.NonPublic | BindingFlags.Instance);
var fi2 = typeof(TestCls).GetField("bb", BindingFlags.NonPublic | BindingFlags.Static);
var fi2 = typeof(TestCls).GetField("bb", BindingFlags.NonPublic | BindingFlags.Static);
var fi3 = typeof(TestCls).GetField("cc", BindingFlags.NonPublic | BindingFlags.Static);

var a = new TestCls();

Console.WriteLine("aa=" + fi.GetValue(a));
Console.WriteLine("bb=" + fi2.GetValue(null));

var cc = (int)fi3.GetValue(null);
Console.WriteLine("cc=" + cc);
if(cc!=444)
throw new Exception("worng value");

fi.SetValue(a, 123);
fi2.SetValue(null, 233);
Console.WriteLine("aa=" + fi.GetValue(a));
Expand Down Expand Up @@ -198,6 +204,9 @@ public class TestCls
int aa = 203;

static int bb = 333;

const int cc = 444;

public TestCls foo(int b)
{
Console.WriteLine("foo" + (aa + b));
Expand Down

0 comments on commit 479793b

Please sign in to comment.