-
-
Notifications
You must be signed in to change notification settings - Fork 372
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
Comments
Thanks @NielsUll Not sure when I can work on it, so any help is appreciated. |
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 [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. |
Accessing a property of an anonymous class fails when going through a dynamic object:
It works fine when the class is named:
dyn.Sub = new MyClass() { Foo = "bar"};
It also works if we introduce an intermediate variable:
Aslo discussed on stack overflow: https://stackoverflow.com/questions/53040955/how-can-i-use-dynamic-variables-with-anonymous-types
The text was updated successfully, but these errors were encountered: