DynamicObject

5 views
Skip to first unread message

kaveh_shahbazian

unread,
Mar 2, 2009, 3:16:27 AM3/2/09
to LinFu.Framework
I saw DynamicObject on Codeproject and then I have got LinFu from
Google code. But it seems DynamicObject is not an official part of
LinFu. Is it so?

Best Regards

Philip_L

unread,
Mar 2, 2009, 9:04:16 PM3/2/09
to LinFu.Framework
Hi Kaveh,

DynamicObject is implemented in LinFu 1.0, but I haven't been able to
implement in 2.0 just yet. LinFu 2.0 does support duck typing,
however, so if you had an interface named IFoo like this:

public class IFoo
{
void DoSomething();
}

...and you had a "Foo" object that looked like this:

public class Foo
{
public void DoSomething()
{
// ...
}
}

you can have the Foo class act like it inherits from the IFoo
interface by using the following code:

var target = new Foo();
var foo = target.CreateDuck<IFoo>();

It's not quite the functionality you would expect from the original
DynamicObject, but if you really need the DynamicObject implemented in
2.0, I'm certainly open to that possibility--either way, I hope that
helps :)


// Treat it like an IFoo instance
foo.DoSomething();


The CreateDuck() extension method is located in
On Mar 2, 4:16 pm, kaveh_shahbazian <kaveh.shahbaz...@gmail.com>
wrote:

kaveh_shahbazian

unread,
Mar 3, 2009, 2:34:51 PM3/3/09
to LinFu.Framework
Hi Philip

1 - Thanks for your reply. This duck facility is not what I was
looking for; yet it is very interesting to me (In fact it is much like
the "Dynamic Interface" feature which was supposed to be in VB 9.0,
then had been cut).

2 - As you have spoke of a 'duck'; will LinFu have interoperability
facilities for handling DLR? As in case for DynamicObject some uses
come to mind.

3 - As just a little description of what I was trying to do with
DynamicObject: Handling command line arguments! Believe it or not this
has been always one of most fascinating problems to me which in turn
has led me to parsers and ... (just some story here). I wanted to use
DynamicObject for a simple kind of structural typing: If object
fulfill protocol A (a C# interface) then I can do X.

Regards

Philip Laureano

unread,
Mar 3, 2009, 3:31:27 PM3/3/09
to linfufr...@googlegroups.com
Hi Kaveh,

I'll do my best to answer your questions. Here we go:

1. AFAIK, VB9 does support late binding with the Option Strict option turned off, so it does allow you to do duck typing on a per-method basis rather than adapt to an entire interface type at once.

2. Once the DLR is officially released, LinFu will use the DLR to do its late-bound calls instead of using reflection. Right now, LinFu does allow you to override its own late-binding routines, but for now, I'll spare you the gory details of how it can be done. :)

3.  It sounds like you need to use the DynamicObject.LooksLike<T> method to analyze whether or not a particular type matches the structure of a particular interface. Right now, LinFu v2.0 has the ability to analyze whether or not a particular method meets a particular structure, but I have yet to extend it so that you can take two types and check if they are structurally compatible. I'll tag that as a feature for the next release.

On a separate note, if you're really interested in doing command line parsing, I have a separate project located here that has a library called NDescent that you can use to parse your command line arguments and do something meaningful with the input, and that might be just what you're looking for...

gminorcoles

unread,
Mar 17, 2009, 4:28:40 PM3/17/09
to LinFu.Framework
bummer. the reason I found LinFu was that I want to create a
lightweight linear genetic programming api with it. Ignoring cool-
sounding terms like genetic programming, I basically wanted to load a
list of MethodInfo objects from all the dlls referenced in some IOC
container, and then use random selection to combine those methods with
parameters of the correct types, and simply execute all the statements
from start to finish linearly, thus the linear part.

I was seriously hoepful that DynamicObject would shorten the path to
this. The idea is that the methods to be combined are largely already
written by users, I am trying to find good ways to call them and pass
in the results to the next expression down the line.

Are there any features of LinFu that would be useful in constructing
such a system? I wrote something barebones using dynamic code
generation but it is a huge pain, I don't want to focus on
implementing the meta-programming part, I just want to do the
productive part.

On Mar 3, 4:31 pm, Philip Laureano <philip.laure...@gmail.com> wrote:
> Hi Kaveh,
>
> I'll do my best to answer your questions. Here we go:
>
> 1. AFAIK, VB9 does support late binding with the Option Strict option turned
> off, so it does allow you to do duck typing on a per-method basis rather
> than adapt to an entire interface type at once.
>
> 2. Once the DLR is officially released, LinFu will use the DLR to do its
> late-bound calls instead of using reflection. Right now, LinFu does allow
> you to override its own late-binding routines, but for now, I'll spare you
> the gory details of how it can be done. :)
>
> 3.  It sounds like you need to use the DynamicObject.LooksLike<T> method to
> analyze whether or not a particular type matches the structure of a
> particular interface. Right now, LinFu v2.0 has the ability to analyze
> whether or not a particular method meets a particular structure, but I have
> yet to extend it so that you can take two types and check if they are
> structurally compatible. I'll tag that as a feature for the next release.
>
> On a separate note, if you're really interested in doing command line
> parsing, I have a separate project located
> here<http://code.google.com/p/tahogen/svn/tahogen/trunk%20that>that
> has a library called NDescent that you can use to parse your command
> line arguments and do something meaningful with the input, and that might be
> just what you're looking for...
>
> On Wed, Mar 4, 2009 at 3:34 AM, kaveh_shahbazian <kaveh.shahbaz...@gmail.com

Philip Laureano

unread,
Mar 30, 2009, 10:02:02 PM3/30/09
to linfufr...@googlegroups.com
DynamicObject might not be able to help you here, but LinFu 2.0 does have a few things that might help you match a set of arbitrary arguments with the correct method that can invoke those arguments.
LinFu has a class called the MethodFinder class that lets you search for a method based on its signature. For example, lets say I had an array of MethodInfo objects that I wanted to search:


IEnumerable<MethodInfo> methods = ... // Insert  your dynamically generated methods here

var finder = new MethodFinder<MethodInfo>();
// Search for a method that holds two strings
var yourArguments = new object[]{"foo", "bar"};
var context = new MethodFinderContext(yourArguments);

var bestMatch = finder.GetBestMatch(context);

Now what makes this interesting is that you can give it any array of methods and it will give you the method that is most compatible with the "foo" and "bar" arguments. The only thing left for you to do would be to populate the array of methods itself, and that should be a little bit easier to do than having to do the dispatch code on your own. Anyway, try it out, and let me know if it helps. :)

Regards,

Philip Laureano

p.s. Sorry for the really late reply!
Reply all
Reply to author
Forward
0 new messages