Dynamic Code Execution

Hello everybody,

is there a way to execute some dynamic code in MSBuild

I need to assign an expression which should be evaluated at run-time (i.e. when the script is being parsed or executed):

<SomeNiceTask
StartDate = DateTime.Today.AddDays(-1).AddHours(2).ToString() />

Is it possible or do I have to write a custom task for that :-|

Ondrej



Answer this question

Dynamic Code Execution

  • Simon Goldshmid

    Thank you guys.

    After all I wrote a small task which does exactly what I want :-|

    Time task from the MSBuild Community just formats current date and time and my aim has been to manipulate current date and time according various inputs (subtracting/adding dates, hours and minutes).

    As I've said, I gave up massaging other tasks and decided to write my own for this only purpose.

    P.S. to MSBuild authors: Nevertheless, I think that MSBuild should have a native taks for evaluating simple expressions. It's not that difficult to implement it ;-).

    Ondrej


  • John McMillion

    ondrejsv,

    I very much need your code which dynamically executes code at runtime which comes from database for me.

    Like, DateTime.Today.AddDays(-30).Month

    Would you please post your full code here so that I can use the same. I have the stringest deadlines, so plz post your full code.

    Regards

    Sujay


  • Horst Klein

    Hi cmumford,

    thank you for the answer, but although I can run any code within the Script task from Community package by tigris, I can't assign computed value to an attribute in another task (or at least I did not figure it out).

    Ondrej


  • dreameR.78

    Yes. There is an open source project that has a collection of custom MSBuild tasks. You can download it from http://msbuildtasks.tigris.org/.

    I believe that the "Script" task will do what you want.


  • lebedinsky

    This is because the real driver for all Tasks is Execute() which only returns a bool to indicate finish state. In the case of the Script Task, Execute() is just a wrapper on CodeDOM. It looks like the wrapper is restricted to in-memory constructions that are not executable. So you won't be able to dynamically generate console apps using Script. I believe you're also limited to C# syntax.

    I can think of two solutions, write a status file or set an environment variable. I think Your best bet is have your dynamic C# write a status a file. You can then use the Framework ReadFromFile Task or the MSBuildCommunity ReadXML to fetch the status after the Task is completed. You could set/get environment variables as well but thats a bit too wacky for my taste.

    Your specific example of dyanmically fetching the date & time can more easily be achieved by using the Time Task in MSBuildCommunity.

  • Dynamic Code Execution