|
|
Comments and Discussions
|
|
 |

|
Hi MB, great work on the DeepObject.
However, I see there is ToString() that serializes to a format similar to Json but it is not Json.
I use Json.Net to serialize but it dumps a lot of other unnecessary metadata as well. Thanks!
|
|
|
|

|
Hello. As you have mentioned, DeepObject's ToString() was intended to provide a string representation of the contents of the instance where it is called, but not to provide it in JSON format.
Having said that seems to me it wouldn't be very hard to build such functionality. At first sight it would be a matter of serialize recursively the properties it holds. But, as I'm not an expert on JSON internals, I'm not sure how would I serialize information about the type of the value such properties store. Maybe you could point me at some place where I can find this information/trick/how-to?
Cheers, Moises
|
|
|
|

|
The only difference for DeepObject should be to auto new ExpandoObject() for child level.
dynamic exp = new ExpandoObject();
exp.Name = "user";
exp.Address = new ExpandoObject();
exp.Address.Line1 = "line1";
result = Newtonsoft.Json.JsonConvert.SerializeObject(exp);
If possible, I suggest that you need not develop your own JSON serializer but using existing libraries such as Newtonsoft.Json. Hope this helps.
|
|
|
|

|
Thanks or sharing this information.
Does the DeepObject implements INotifyPropertyChanged and thus have binding support like ExpandoObject have?
|
|
|
|

|
Hello and thanks for your message.
No, DeepObject does not currently support INotifyPropertyChanged: I have not yet figure out a nice syntax that could be used in a "multi-level" environment, as it is the use case that DeepObject was designed for.
By "multi-level dynamic object" I mean that its first-level properties can also be dynamic (DeepObject) ones, and so they could carry their own second-level dynamic properties, that in turn could carry their own third level... and so forth, permitting to add and use them in a dynamic, natural and on-demand fashion.
It does already support other interfaces (ISerializable, ICloneable, IDisposable), so I will be happy to implement INotifyPropertyChanged if there is any suggestion on what would be the most handy sintax to use this feature afterwards.
Thanks, Moises
|
|
|
|

|
Interesting stuff, thanks.
|
|
|
|
|
|
|
|
|

|
This was fine when dynamic first came out, but would expect an article with more depth if it is published today. Way to basic to be worth republishing. Also refers to the new C# 4.0, which is no longer new.
|
|
|
|

|
Thanks for your comment.
Let me mention that this piece is part of a bigger ORM solution I'm actually publishing now, so my intention was that, instead of "polluting" the ORM article's descrition with this piece, it has sense, in my modest opinion, to have this new version published in its own one.
Anyhow, I have not seen any similar solution elsewhere.
Thanks, Moises.
|
|
|
|

|
spelling error: ExpandoObjetc
|
|
|
|

|
Hello ,
How are you today ? .
I hope you are fine and all is well with you, My name is miss glory i saw your profile at codeproject.com which really interest me and i decide to communicate with you, and it will please me if you will be my friend, Here is my direct email address (glory_west10@yahoo.in),so that i will tell you more about me and my picture hope to hear from you
Glory.
( glory_west10@yahoo.in)
|
|
|
|

|
Difficult to understand but the concept of "dynamic" is amazing! I had a long standing issue which i promptly solved with "dynamic". Thanks!
|
|
|
|

|
Appreciate your comment.
Maybe the best way to explain it in this context is that instead of "coding in advance" the properties and methods of an object, the concept of "dynamic" permits you to define such properties and methods when you are actually in run-time. Purist will kill me for this explanation, but hope it helps you.
Thanks, Moisés.
|
|
|
|

|
I do find this amazing, however what is even more amazing is how old patterns, technologies are finally coming back. Back in 2000 I developed distrubuted com+ applications, all of which was n-tier DNA architecture using Com+.
The middleware architecture provided an interface which accepted XML (before message queue came out) which received the XML request. This requesrt contain the method name, parameters. THis XML stream would be validated within BL objects and if ok would late binding and call the relevant object with its associated parameters.
This article discusses late binding and its ability to solve some issue, but it existed 10 years ago in Com+.
|
|
|
|

|
I am getting this
The name 'TypeHelper' does not exist in the current context
|
|
|
|

|
Thanks for your message John. I have to apologize, I didn't include the TypeHelper.cs file, an unforgivable slip.
Fortunately, you only need a few lines, as follows:
public static class TypeHelp {
internal static Delegate _CreateConverterDelegate( Type sourceType, Type targetType ) {
var input = Expression.Parameter( sourceType, "input" );
Expression body;
try { body = Expression.Convert( input, targetType ); }
catch( InvalidOperationException ) {
var conversionType = Expression.Constant( targetType );
body = Expression.Call( typeof( Convert ), "ChangeType", null, input, conversionType );
}
var result = Expression.Lambda( body, input );
return result.Compile();
}
public static object ConvertTo( object source, Type targetType ) {
if( targetType == null ) throw new ArgumentNullException( "Target Type" );
Type sourceType = ( source == null ) ? typeof( object ) : source.GetType();
Delegate converter = _CreateConverterDelegate( sourceType, targetType );
return converter.DynamicInvoke( source );
}
public static T ConvertTo<T>( object source ) {
T target = (T)ConvertTo( source, typeof( T ) );
return target;
}
}
Hope it helps. Best regards, M
|
|
|
|

|
that did it. thanks. great article.
|
|
|
|

|
complex issue but not explained up to the mark
|
|
|
|

|
can it binding property in silverlight?
|
|
|
|

|
Thanks for your message. I haven't tried - but as far as it does support C# dynamics, if it is the case, I don't see why not.
|
|
|
|

|
When I first read about this new feature in .NET 4.0 I thought OMG MSFT is actually dumbing down C#. I couldn't for the life of me figure out the reasoning behind this. As a strong supporter of strongly typed languages and the massive benefits they bring, I just thought this wasn't a good idea.
But then having worked in WPF, coming from the Windows Forms world with the excellent support of the .NET wrapper for the System.Windows.Forms.WebBrowser class , and trying to work with the WPF WebBrowser class, I found that MSFT did not decide to add additional .NET Wrapping to improve on the Forms Browser, rather they went the other way and decided to force you to go towards the COM implementation of the browser. As a result of this, there was much more power in parsing the HTML DOM object (IE itself using the "Trident" engine has already parsed the document in order to display it).. So why use a wrapper? The answer was convienience for the .NET world. But many things were missing from the wrapper. Thus articles like this The most complete C# Webbrowser wrapper control. Today, that type of support is somewhat obsolete with the dynamic support.
With the new Dynamic support, one no longer needs to do endless casting to obtain methods and properties for unknown objects. The Dynamic support allows you to just call the method without knowing what type it is. So now one can do this: Dynamic htmldoc = Webbrowser.Document, they type of which will be any supported document type. Most of the time a person will want the HTMLDocument class, but in WPF this is the MSHTML.DocumentClass which requires massive amounts of casting to morph the content into types of which access to any member in the class is possible. In the past that casting was problematic because intellisense doesn't work too well with COM. A developer had to read the References for the MSHTML class and test the code with each new thing added. Addtionally, there was no Immediate Window support to "experiement" when single stepping through code. Quite often Watches on Vars didn't work either due to COM object states not allowing it. Countless hours were spent trying to just "understand" how to work with MSHTML. Not any longer because the dynmaic object allows one to do one sided casting...
So now if I want an interface, all that's needed is this Dynamic interface3 = (mshtml.IHtmldocument3)document. I can now morph the document and call methods without casting on the left side. Here's another example, I can use Dynamic to get all htmlelements like this Dynamic elements = document.all. There is no left side casting. I can now test for null and parse accordingly. I can allow call any members of the elements object without casting. Using Dynamic support for MSHTML object is really nice.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
A class to create multi-level dynamic objects in C# 4.0
| Type | Article |
| Licence | CPOL |
| First Posted | 28 May 2010 |
| Views | 37,159 |
| Downloads | 1,013 |
| Bookmarked | 98 times |
|
|