Monday, July 12, 2004

Default Binder for class

Let’s write a wrapper around an XML element. The wrapper represents properties for element’s attributes.

XmlElement e;
XmlWrapper w = new XmlWrapper (e);
String s = @”font-face : sans-serif”;
w.style = s; statement 1

In the example the latter statement should effectively call

e.SetAttribute (“style”, s);

A generated class is a good thing, but what about more dynamic approach? A custom binder could help. But in this situation accessing the methods would be neither readable nor writable:

typeof(XmlWrapper).InvokeMember (“style”, SetField, XmlWrapperBinder, e, {s}); statement 2

Assigning a binder to the class XmlWrapper could help:

[Binder (XmlBinder)]
Class XmlWrapper {…}

Then compiler intelligently places statement 2 in place of statement 1


This page is powered by Blogger. Isn't yours?