Je veux convertir Expression<Func<Loan, bool>>
en chaîne et vice versa. puis-je le faire? comment peut mettre en œuvre la méthode ConvertStringToExpression
?Convertir l'expression <Func <T, bool>> en chaîne et vice versa En C#
internal class Program
{
public class Loan
{
public bool IsActive { get; set; }
}
private static void Main(string[] args)
{
Expression<Func<Loan, bool>> expression = l => !l.IsActive;
var expStr = ConvertExpressionToString(expression);
var exp = ConvertStringToExpression(expStr);
}
public static string ConvertExpressionToString(Expression<Func<Loan, bool>> expression)
{
//return ???
throw new NotImplementedException();
}
public static string ConvertStringToExpression(string expression)
{
//return ???
throw new NotImplementedException();
}
}
Comment voulez-vous que cela fonctionne ?? Et plus important encore, pourquoi voudriez-vous faire cela? –
En résumé: vous devrez analyser la chaîne à l'aide de 'Microsoft.CodeAnalysis.CSharp.SyntaxFactory', obtenir (plus probablement)' Microsoft.CodeAnalysis.CSharp.Syntax.SimpleLambdaExpressionSyntax', obtenir l'expression en fonction de ce que vous y mettez et se termine par une 'System.Linq.Expressions.LambdaExpression'. – Mark
@DavidL Je veux enregistrer des expressions en tant que chaîne (pas xml) dans la base de données. – ArMaN