Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer is a wee bit orthogonal - more about the problem than the technology involved.</p> <p>You could use expression trees - they're nice to work with and have VS syntactic sugar.</p> <p>For serialization you need this library (you'll need to compile it too): <a href="http://expressiontree.codeplex.com/" rel="nofollow">http://expressiontree.codeplex.com/</a> <em>(This works with Silverlight 4 too, apparently.)</em></p> <p>The limitation of expression trees is that they only support Lambda expressions - i.e. no blocks.</p> <p>This isn't really a limitation because you can still define other lambda methods within a lambda and pass them to functional-programming helpers such as the Linq API.</p> <p>I've included a simple extension method to show you how to go about adding other helper methods for functional stuff - the key point is that you need to include the assemblies containing the extensions in your serializer.</p> <p>This code works:</p> <pre><code>using System; using System.Linq; using System.Linq.Expressions; using ExpressionSerialization; using System.Xml.Linq; using System.Collections.Generic; using System.Reflection; namespace ExpressionSerializationTest { public static class FunctionalExtensions { public static IEnumerable&lt;int&gt; to(this int start, int end) { for (; start &lt;= end; start++) yield return start; } } class Program { private static Expression&lt;Func&lt;int, int, int&gt;&gt; sumRange = (x, y) =&gt; x.to(y).Sum(); static void Main(string[] args) { const string fileName = "sumRange.bin"; ExpressionSerializer serializer = new ExpressionSerializer( new TypeResolver(new[] { Assembly.GetExecutingAssembly() }) ); serializer.Serialize(sumRange).Save(fileName); Expression&lt;Func&lt;int, int, int&gt;&gt; deserializedSumRange = serializer.Deserialize&lt;Func&lt;int, int, int&gt;&gt;( XElement.Load(fileName) ); Func&lt;int, int, int&gt; funcSumRange = deserializedSumRange.Compile(); Console.WriteLine( "Deserialized func returned: {0}", funcSumRange(1, 4) ); Console.ReadKey(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload