Ask a questionAsk a question
 

AnswerOslo and DLR in .Net 4.0

  • Tuesday, October 20, 2009 12:56 PMDonBaechtel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is it possible to use the Dynamic Language Runtime and Expression Trees from .Net 4.0 to help build a runtime for Oslo output?
    Does this make sense?
    Is there an example of this?

Answers

  • Tuesday, October 20, 2009 3:20 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yeah it's definitely possible. I have done this for a project of mine already, but I didn't do it from the repository just from straight M Graph output. It's definitely not simple though I will tell you. Doing it from the repository would probably be about the same except that you're getting your metadata from somewhere else.

    Here is my sample:
    http://metasharp.codeplex.com/SourceControl/changeset/view/32489#398885 // template for dynamically transforming a Song DSL into a lambda.

    The gist is, use a Visitor pattern to visit your graph and build up an expression tree then compile it into a delegate. The thing I made lets you compile textual DSLs into linq expressions using a T4 like language for doing transforms. Or you can do the language itself. such as:

    var result = pipeline.Eval<int>("x + y", new { x = 3, y = 7 });
    Assert.AreEqual(21, result);

    So it's definitely possible! The only limitation is that you cannot create new Types using purely linq expressions. You can create new types using DynamicAssembly but then you have to emit your own IL, you cannot use the linq expressions to create method bodies for DynamicMethods (at least I couldn't figure it out). Which also means you cannot access "this" or members of this from within a lambda but you can make implicit references by adding parameters and constants. There are still lots of cool things you can do though.

All Replies

  • Tuesday, October 20, 2009 3:20 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yeah it's definitely possible. I have done this for a project of mine already, but I didn't do it from the repository just from straight M Graph output. It's definitely not simple though I will tell you. Doing it from the repository would probably be about the same except that you're getting your metadata from somewhere else.

    Here is my sample:
    http://metasharp.codeplex.com/SourceControl/changeset/view/32489#398885 // template for dynamically transforming a Song DSL into a lambda.

    The gist is, use a Visitor pattern to visit your graph and build up an expression tree then compile it into a delegate. The thing I made lets you compile textual DSLs into linq expressions using a T4 like language for doing transforms. Or you can do the language itself. such as:

    var result = pipeline.Eval<int>("x + y", new { x = 3, y = 7 });
    Assert.AreEqual(21, result);

    So it's definitely possible! The only limitation is that you cannot create new Types using purely linq expressions. You can create new types using DynamicAssembly but then you have to emit your own IL, you cannot use the linq expressions to create method bodies for DynamicMethods (at least I couldn't figure it out). Which also means you cannot access "this" or members of this from within a lambda but you can make implicit references by adding parameters and constants. There are still lots of cool things you can do though.