nrother / dynamiclua

Wrapper for NLua, making access to lua more idomatic from .NET. Heavily using the "dynamic" keyword.
63 stars 10 forks source link

Private members should perhaps not be readable #16

Closed d-led closed 10 years ago

d-led commented 10 years ago

As it seems, privates can be read from. The assertion fails:

lua.obj = new SomeObject();
string answer = lua("return obj.name")[0];
answer.Should().NotBe(new SomeObject().Name);

with

public class SomeObject
{
    string name = "SomeName";
    public string Name { get { return name; } }
}
nrother commented 10 years ago

This issue is a bit complicated, but the main problem is not in DynamicLua but in NLua: In this line of NLua BindingFlags.IgnoreCase it set. (According to the comment they had even BindingFlag.Private in there, but removed it). Since your private member is called name and your public property is called Name that one is found, since they search case-invariant.

So a simple workaround is to rename the property. Interestingly then your code returns name, instead of throwing an exception, which is a bit strange. (BTW: You don't need that [0], an DynamicArray containing exactly one element can be implicitly converting to it's containing value.)

Please note that the code throws the correct exception, when the access goes through DynamicLua, which is even a bit simpler that to return the value from Lua.

string answer = lua.obj.name

So this basically a bad design decision in NLua... I'll close this issue as wont-fix, feel free to report it to NLua!

nrother commented 10 years ago

I just reported this issue to NLua: https://github.com/NLua/NLua/issues/98

d-led commented 10 years ago

ok!

nrother commented 10 years ago

They fixed it, so you could try using DynamicLua with a new version of the NLua.dll. Probably I will also push fixed version soon (or solve the dependency problem some other way, especially for Mono...)

d-led commented 10 years ago

great!