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

Cannot access anonymous class in properties of dynamic object #93

Open
NielsUll opened this issue Oct 30, 2018 · 2 comments
Open

Cannot access anonymous class in properties of dynamic object #93

NielsUll opened this issue Oct 30, 2018 · 2 comments

Comments

@NielsUll
Copy link

Accessing a property of an anonymous class fails when going through a dynamic object:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'object' does not contain a definition for 'Foo'

[Test]
public void Get_Property_of_a_nested_anonymous()
{
    dynamic dyn = new ExpandoObject();
    dyn.Sub = new { Foo = "bar"};
    var interpreter = new Interpreter()SetVariable("dyn", (object)dyn);
    Assert.AreEqual(dyn.Sub.Foo, interpreter.Eval("dyn.Sub.Foo"));
}

It works fine when the class is named: dyn.Sub = new MyClass() { Foo = "bar"};
It also works if we introduce an intermediate variable:

    dynamic dyn = new ExpandoObject();
    dyn.Sub = new { Foo = "bar"};
    var interpreter = new Interpreter().SetVariable("dyn", dyn);
    interpreter.SetVariable("sub", interpreter.Eval("dyn.Sub"));
    Assert.AreEqual(dyn.Sub.Foo, interpreter.Eval("sub.Foo"));

Aslo discussed on stack overflow: https://stackoverflow.com/questions/53040955/how-can-i-use-dynamic-variables-with-anonymous-types

@davideicardi
Copy link
Member

Thanks @NielsUll Not sure when I can work on it, so any help is appreciated.

@metoule
Copy link
Contributor

metoule commented Jun 10, 2021

In C#, anonymous types are internal types, which mean they're only visible to the assembly in which they're defined. In your scenario, the defining assembly (yours) is not the same as the DynamicExpresso assembly, which means the binder can't find its properties. There's a simple example here: https://stackoverflow.com/questions/2630370/c-sharp-dynamic-cannot-access-properties-from-anonymous-types-declared-in-anot .

You can see the same behavior when trying to use a private class as Sub:

[Test]
public void Get_Property_of_a_nested_internal_class()
{
	dynamic dyn = new ExpandoObject();
	dyn.Sub = new MyClass { Foo = "bar" };
	var interpreter = new Interpreter().SetVariable("dyn", (object)dyn);
	Assert.AreEqual(dyn.Sub.Foo, interpreter.Eval("dyn.Sub.Foo"));
}

private class MyClass
{
	public string Foo { get; set; }
}

If you change the class to be public instead, then the test pass.

Based on this analysis, I'm not sure this can be fixed in DynamicExpresso.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants