views:

33

answers:

1

The best way, of course, is to convert the method to a property. But I can't do that here --

We have an API from someone else, and we've added an extension method to one of the objects. We need at the string this method returns in a data-binding situation (a GridView).

It doesn't seem that we can add an extension property (man, that would have been really nice...), so I have this method, and I want the cleanest way possible to render it in data-binding:

While I can do this with a property:

<%# Eval("MyProperty") %>

I have to do this with a method:

<%# ((MyClass)Container.DataItem).MyExtensionMethod() %>

This is how I've done it in the past, but is there a cleaner way than that? I can't change the class (it's not mine), and I can't add an extension property, so I'm stuck with this method.

A: 

I guess you could write your own method that took the Container object and the name of the method as a string, then used reflection to invoke the method. It'd look something like this in the end:

<%# Call(Container, "MyExtensionMethod") %>

I doubt you can get much cleaner than that, since Eval is a special case which is rewritten at compile-time.

Blixt