NJection.LambdaConverter
is an open-source .NET assembly for converting delegates
resolved from methods/constructors to expression trees.
Install-Package NJection.LambdaConverter
public class Program
{
private static void Main(string[] args) {
var lambda = Lambda.TransformMethodTo<Func<string, int>>()
.From(() => Parse)
.ToLambda();
}
public static int Parse(string value) {
return int.Parse(value);
}
}
public class Sample
{
public int Parse(string value) {
return int.Parse(value);
}
}
public class Program
{
private static void Main(string[] args) {
var sample = new Sample();
var lambda = Lambda.TransformMethodTo<Func<string, int>>()
.From(() => sample.Parse)
.WithContextOf<Sample>(sample)
.ToLambda();
}
}
public class Sample
{
public int Value { get; set; }
public Sample(int i) {
Value = i;
}
}
public class Program
{
private static void Main(string[] args) {
var types = new Type[] { typeof(int) };
var ctor = typeof(Sample).GetConstructor(types);
var resolver = new ConstructorResolver(ctor);
var lambda = Lambda.ResolveConstructorTo<Func<int, Sample>>(resolver)
.ToLambda();
}
}
public class Program
{
private static void Main(string[] args) {
var lambda = Lambda.TransformMethodTo<Func<string, DateTime>>()
.From(() => Parse<DateTime>)
.ToLambda();
}
private static T Parse<T>(string value) where T : struct {
return (T)Enum.Parse(typeof(T), value);
}
}
public class Program
{
private delegate int CustomDelegate(string value);
private static void Main(string[] args) {
var lambda = Lambda.TransformMethodTo<CustomDelegate>()
.From(() => Parse)
.ToLambda();
}
private static int Parse(string value) {
return int.Parse(value);
}
}
Mono.Cecil
ICSharpCode.Decompiler
ICSharpCode.NRefactory
ICSharpCode.NRefactory.CSharp
NJection.LambdaConverter
is distributed under the MIT License.