From b6e62c90dff79f008432ffedea2d973ff701100c Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sun, 9 Sep 2018 13:45:04 +0200 Subject: [PATCH 1/2] add failing tests --- Tests/PrivateTest.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/PrivateTest.cs b/Tests/PrivateTest.cs index e0c2b6a..b5f8e40 100644 --- a/Tests/PrivateTest.cs +++ b/Tests/PrivateTest.cs @@ -65,6 +65,21 @@ public void TestCacheableExposePrivateMethodViaType() var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Test", context: typeof(TestWithPrivateMethod)); Assert.That( tCachedInvoke.Invoke(tTest), Is.EqualTo(3)); } + + [TestCase(typeof(TestNestedWithPrivateStaticField))] + [TestCase(typeof(TestNonPublicWithPrivateStaticField))] + [TestCase(typeof(TestWithPrivateStaticField))] + public void TestPrivateStaticField(Type type) + { + var staticContext = InvokeContext.CreateStatic; + var hello = Dynamic.InvokeGet(staticContext(type), "Hello"); + Assert.That(hello, Is.EqualTo("World")); + } + + public class TestNestedWithPrivateStaticField + { + private static string Hello => "World"; + } } public class TestWithPrivateMethod @@ -74,4 +89,14 @@ private int Test() return 3; } } + + internal class TestNonPublicWithPrivateStaticField + { + private static string Hello => "World"; + } + + public class TestWithPrivateStaticField + { + private static string Hello => "World"; + } } From 72e404f04a510ba84f5325362a11961bda5db51a Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sun, 9 Sep 2018 14:56:16 +0200 Subject: [PATCH 2/2] demonstrate how to get private static property --- .../Optimization/InvokeHelper-Regular.cs | 37 +++++-------------- Tests/PrivateTest.cs | 8 ++++ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs b/Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs index e981513..3fc0191 100644 --- a/Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs +++ b/Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs @@ -446,36 +446,17 @@ internal static object InvokeGetCallSite(object target, string name, Type contex if (staticContext) //CSharp Binder won't call Static properties, grrr. { var tStaticFlag = CSharpBinderFlags.None; - if ((target is Type && ((Type)target).GetTypeInfo().IsPublic)) - { - tBinder = () => Binder.InvokeMember(tStaticFlag, "get_" + name, - null, - context, - new List - { - CSharpArgumentInfo.Create( - CSharpArgumentInfoFlags.IsStaticType | - CSharpArgumentInfoFlags.UseCompileTimeType, - null) - }); - - tBinderType = typeof (InvokeMemberBinder); - tKnownType = KnownMember; - } - else - { - tBinder = () => Binder.GetMember(tStaticFlag, name, - context, - new List - { - CSharpArgumentInfo.Create( - CSharpArgumentInfoFlags.IsStaticType, null) - }); + tBinder = () => Binder.GetMember(tStaticFlag, name, + context, + new List + { + CSharpArgumentInfo.Create( + CSharpArgumentInfoFlags.IsStaticType, null) + }); - tBinderType = typeof(InvokeMemberBinder); - tKnownType = KnownMember; - } + tBinderType = typeof(InvokeMemberBinder); + tKnownType = KnownMember; } else { diff --git a/Tests/PrivateTest.cs b/Tests/PrivateTest.cs index b5f8e40..1bd737f 100644 --- a/Tests/PrivateTest.cs +++ b/Tests/PrivateTest.cs @@ -72,6 +72,14 @@ public void TestCacheableExposePrivateMethodViaType() public void TestPrivateStaticField(Type type) { var staticContext = InvokeContext.CreateStatic; + try + { + Dynamic.InvokeSet(staticContext(type), "Hello", null); + } + catch (RuntimeBinderException) + { + + } var hello = Dynamic.InvokeGet(staticContext(type), "Hello"); Assert.That(hello, Is.EqualTo("World")); }