问题
How can you use Dynamic Language Runtime (DLR) or something else to mimic System.Dynamic.DynamicObject in SILVERLIGHT 3 (!) and WP7 (no C# 4.0!)..
What I want to do is implement automatic dynamically created properties for databinding.
回答1:
The DLR can be used with WP7.
Iron Ruby supports Windows Phone 7 from version 1.1.
DynamicObject
was available as an extension prior to .Net 4.0
If you're happy using a language other than C# then this could be an option.
There's a good article on using IronRuby on Windows Phone 7 at http://msdn.microsoft.com/en-us/magazine/ff960707.aspx.
As an alternative, could you use anonymous types, rather than dynamic ones? I assume that there isn't an infinite number of options you're potentially binding to.
If you do want to use anonymous types you must set
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Windows")]
to enable the framework to resolve the bindings.
Presumably you're getting you dynamic objects from an external system. Could you not add a wrapper layer between your app and the external system to turn the objects into POCOs rather than dynamic objects.
回答2:
As you mentioned, DLR
with .Net 4.0
and C# 4.0
is not available in WP7
. It means you can't do it. At least not easily.
Silverlight does not allow custom markup extensions (things between {}
). So, that's not an option as well.
What you could do is create custom Behaviors
for binding and make them work with Dictionaries
that can hold as many properties and values.
来源:https://stackoverflow.com/questions/4603017/how-to-use-something-similar-to-dynamicobject-in-silverlight-3-wp7