c# - List<dynamic> elements have fields but I cannot access them. Why? -
i need loop on list<dynamic>
objects.
the list's objects have values, reason, not able access of dynamic object fields. below screenshot of debug window:
there can see object contains fields (such alias, id, name, etc).
i tried both casting idictionary<string, object>
, expandoobject
, no avail. did not face such thing before: failing access existing fields in dynamic
object when exist.
what wrong here?
the code throwing microsoft.csharp.runtimebinder.runtimebinderexception
message stating {"'object' not contain definition 'name'"}.
the list created adding anonymously-typed objects, this:
return new list<dynamic>(fields.select(field => new { id = field.id, alias = field.alias, name = field.name, type = field.type, value = field.value, sortorder = field.sortorder }));
where fields
icollection<field>
, strongly-typed collection.
the telling part exception:
{"'object' not contain definition 'name'"}.
this indicates runtime binder not capable of accessing type you're passing in dynamic
(since dynamic
enforce visibility rules).
the cause of you're creating anonymous type in different assembly 1 you're subsequently reading - since anonymous types declared internal
, consuming assembly cannot access it, causing error message above.
contrast usual case of runtime binder exceptions:
'<>f__anonymoustype0< string >' not contain definition 'name'
edit:
a possible solution problem use internalsvisibletoattribute
on assembly containing anonymous type. however, code smell - other use of internalsvisibletoattribute
or internal
itself.
a better way make sure don't pass anonymous types on assembly boundaries - after all, shouldn't used outside of method originated from; fact implementation detail of .net - didn't have way same thing. could change in future versions, making internalsvisibletoattribute
solution doubly unreliable.
the way code using dynamic
suggests team has flawed assumptions how dynamic
works , how it's supposed used. note how actual runtime type of list<dynamic>
list<object>
. same goes arguments of type dynamic
(which again object
, albeit marked dynamicattribute
). , in fact, dynamic
- it's way handle runtime dynamic dispatch - it's not property of type or anything, it's way invoke whatever you're trying invoke. c#, dynamic
allows skip of compiler checks when working dynamic types, , generates code handle dispatch automatically, of happens inside method use dynamic
keyword - if used list<object>
, end result same.
in code, there's no reason not use simple static types. dynamic typing doesn't give benefits, apart effort code types themselves. if co-workers don't that, well, should present better solution - problem quite obvious, , it's need deal with.
much worse, explicitly hides context, all type information. that's not want in api, internal or not! if want hide concrete types being used, why not - should still expose interface instead. suspect reason why anonymous types can't implement interfaces - encourage go entirely wrong way.
Comments
Post a Comment