Serialize.Linq is a .NET library that provides functionality to serialize and deserialize LINQ expressions. This library is useful in scenarios where you need to send LINQ expressions over the wire or persist them in a database.
- Serialize and deserialize LINQ expressions to XML, JSON, and binary formats.
- Supports various types of expressions, including binary, unary, member access, lambda, and more.
- Extensible design that allows you to add support for custom expressions.
- BinaryFormatSerializer has been removed due to security concerns with
BinaryFormatter. For more details, see the BinaryFormatter Security Guide.
You can install Serialize.Linq via NuGet:
Install-Package Serialize.Linq
Here's a simple example of how to use Serialize.Linq:
// Create an expression
Expression<Func<int, bool>> expression = num => num < 5;
// Create a serializer
var serializer = new ExpressionSerializer(new JsonSerializer());
// Serialize the expression
string serializedExpression = serializer.SerializeText(expression);
// Deserialize the expression
var deserializedExpression = serializer.DeserializeText(serializedExpression);Deserializing an expression reconstructs Types by name from the payload. When the payload is
untrusted, supply an ITypeFilter on an ExpressionContext to allow-list the permitted types —
the equivalent of BinaryFormatter's SerializationBinder. Any type the filter rejects throws a
TypeNotAllowedException.
using Serialize.Linq;
using Serialize.Linq.TypeFilters;
// Only allow the types your expressions actually use.
var filter = new AllowedTypesFilter(typeof(int), typeof(bool), typeof(object), typeof(Func<,>))
.AllowNamespace("MyApp.Models");
var context = new ExpressionContext(filter);
var deserializedExpression = serializer.DeserializeText(serializedExpression, context);AllowedTypesFilter matches explicit types (use the open definition for generics, e.g.
typeof(List<>)) and whole namespaces; DelegateTypeFilter wraps an arbitrary
Func<Type, bool> predicate. The filter is checked for every resolved type, including each
generic argument. With no context/filter, all types resolve as before.
We welcome contributions to Serialize.Linq! If you'd like to contribute, please fork the repository, make your changes, and submit a pull request. If you're not sure where to start, take a look at our open issues.
For bugs: make sure you create a unit test, so it is easier for me to reproduce and fix it.
You can always buy me a coffee ☕.
Serialize.Linq has a comprehensive test suite. You can run the tests using your preferred test runner.
- .NET 10.0
- .NET 9.0
- .NET 8.0
- .NET 7.0
- .NET 6.0
- .NET 4.8
- .NET 4.8.1
- .NET Standard 2.0
- .NET Standard 2.1
Serialize.Linq is licensed under the MIT License. See the LICENSE file for more details.