Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INCOMPLETE!] Getting non-public static property #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
{
CSharpArgumentInfo.Create(
CSharpArgumentInfoFlags.IsStaticType |
CSharpArgumentInfoFlags.UseCompileTimeType,
null)
});

tBinderType = typeof (InvokeMemberBinder);
tKnownType = KnownMember;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This binder only works for public type. The else only works for private type when a property is set-first...

else
{

tBinder = () => Binder.GetMember(tStaticFlag, name,
context,
new List<CSharpArgumentInfo>
{
CSharpArgumentInfo.Create(
CSharpArgumentInfoFlags.IsStaticType, null)
});
tBinder = () => Binder.GetMember(tStaticFlag, name,
context,
new List<CSharpArgumentInfo>
{
CSharpArgumentInfo.Create(
CSharpArgumentInfoFlags.IsStaticType, null)
});

tBinderType = typeof(InvokeMemberBinder);
tKnownType = KnownMember;
}
tBinderType = typeof(InvokeMemberBinder);
tKnownType = KnownMember;
}
else
{
Expand Down
33 changes: 33 additions & 0 deletions Tests/PrivateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ 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;
try
{
Dynamic.InvokeSet(staticContext(type), "Hello", null);
}
catch (RuntimeBinderException)
{

}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this try..catch causes all test cases to fail but I do not understand why

var hello = Dynamic.InvokeGet(staticContext(type), "Hello");
Assert.That(hello, Is.EqualTo("World"));
}

public class TestNestedWithPrivateStaticField
{
private static string Hello => "World";
}
}

public class TestWithPrivateMethod
Expand All @@ -74,4 +97,14 @@ private int Test()
return 3;
}
}

internal class TestNonPublicWithPrivateStaticField
{
private static string Hello => "World";
}

public class TestWithPrivateStaticField
{
private static string Hello => "World";
}
}