Serializing and Deserializing Expression Trees in C#
Asked Answered
C

7

97

Is there a way to Deserialize Expressions in C#, I would like to store Expressions in a Database and load them at run time.

Cita answered 20/10, 2008 at 10:4 Comment(3)
what kind of expressions? Regex?Eminent
I would assume System.Linq.Expression type expressions, given the "trees".Questionless
you can use codeproject.com/Articles/851187/…Gibert
K
49

I continued work on the library that was mentioned by Serializing and Deserializing Expression Trees in C#

It looks like the project was abandoned (2008) but I did some work on it and now it works with .NET 4.0 and Silverlight. I made bug fixes to their code and also made it more DAL-independent.

http://expressiontree.codeplex.com/

Karalee answered 14/6, 2011 at 22:15 Comment(4)
+1 for coming up with a working solution. I tried using the original library on MSDN and it couldn't deserialize a simple binary expression node. Your project works great. Thanks!Rule
Is it project on Nuget ?Gibert
@MohammadDayyan, Microsoft has been indecisive and inconsistent on its package management tool. Sometimes ASP.NET CORE packages can be installed with Nuget, in other cases e.g. a MongoDB driver won't be available via NuGet. Still at least Microsoft had the insight to allow NPM to manage packages. More likely to put this on NPMKaralee
@RepoMan solving a problem is worth so much more to me when it helps someone. Maybe you can contribute some documentation on usage - let's talk..Karalee
M
17

There's an project on GitHub/Nuget called MetaLinq that aims to make working with expression tress more easy.

It converts between normal Expressions and 'EditableExpressions' which are mutable and fully Serializable, so can be used with Json, Xml, Binary etc.

Also check this blog post for more info.

Melon answered 20/10, 2008 at 15:11 Comment(0)
F
14

Other option is the Expression Tree Serialization project on code.msdn.com - would appear to be more what is needed here?

UPD: Now http://archive.msdn.microsoft.com/exprserialization

Flagpole answered 28/9, 2009 at 9:57 Comment(3)
Expression Tree Serialization code is removed from code gallery code.msdn.microsoft.com/exprserialization, any idea where it is moved to or better alternatives?Lastditch
@Kiran: What do you mean? The code download is still there; check the downloads tab at the top of the page.Epizoon
Yes it is now, probably a temporary thing, the page was throwing a project not found error yesterday when I tried the link above. I see the project and download now. Thanks!!!Lastditch
L
13

have a look at my new library Serialize.Linq. It serializes linq expressions to json, xml and binary.

Leastways answered 22/9, 2012 at 10:3 Comment(6)
have you used this before? how was your experience / find any limitations?Cita
i used a similar version of it in my last project. works good.Leastways
Why would you write something that bleeds the lines between tiers worse? You're promoting the exposure of DAL domain objects to the BAL... unless your library somehow automatically converts DTOs (and the queries against them) to domain objects/queries. I don't see anything in the docs indicating such behavior.Derwent
@M.Babcock: what you want to use in your expressions DTO vs Entities is not part of the logic of the library.Leastways
@Leastways your blog / whole site seems to be gone. has it moved?Overfeed
The link is now fixedLeastways
M
3

Not in full; however, the Dynamic LINQ sample may help a bit. You can certainly serialize an expression to a string (to an extent - generics look a bit screwy), but there is no inbuilt parser.

Mig answered 20/10, 2008 at 10:16 Comment(1)
I can't find out how to serialize an expression to a string using the sample, only the other direction, to take as string and create an expression tree. What method do I call?Phillida
A
2

WCF RIA Services allows for serializing IQuerayble<T>

WCF RIA Services Part 3 – Updating Data

Amalita answered 10/9, 2010 at 7:27 Comment(1)
I believe this is more relevant: silverlightshow.net/items/…Lizarraga
G
1

I was working on a project for serializing expressions. It's OK and you can use it. http://www.codeproject.com/Articles/851187/ServicePredicateBuilder-for-creating-Serializable

Gibert answered 15/8, 2015 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.