Click here to Skip to main content
15,888,521 members
Home / Discussions / C#
   

C#

 
GeneralRe: not working input simulator Pin
Sascha Lefèvre1-Apr-16 6:20
professionalSascha Lefèvre1-Apr-16 6:20 
GeneralRe: not working input simulator Pin
Jonahweird2-Apr-16 1:35
Jonahweird2-Apr-16 1:35 
QuestionTcpListener don't send FIN after client send FIN Pin
Aurélien381-Apr-16 3:13
Aurélien381-Apr-16 3:13 
AnswerRe: TcpListener don't send FIN after client send FIN Pin
Richard Andrew x641-Apr-16 3:40
professionalRichard Andrew x641-Apr-16 3:40 
GeneralRe: TcpListener don't send FIN after client send FIN Pin
Aurélien381-Apr-16 10:46
Aurélien381-Apr-16 10:46 
AnswerRe: TcpListener don't send FIN after client send FIN Pin
Gerry Schmitz1-Apr-16 7:02
mveGerry Schmitz1-Apr-16 7:02 
GeneralRe: TcpListener don't send FIN after client send FIN Pin
Aurélien381-Apr-16 10:38
Aurélien381-Apr-16 10:38 
QuestionMulti threading with datatable in C# Pin
Jaimesh.24111-Apr-16 3:03
Jaimesh.24111-Apr-16 3:03 
GeneralRepost Pin
Sascha Lefèvre1-Apr-16 3:39
professionalSascha Lefèvre1-Apr-16 3:39 
QuestionFont error when using C# code behind to export from crystal report to pdf Pin
botngot8331-Mar-16 21:55
botngot8331-Mar-16 21:55 
AnswerRe: Font error when using C# code behind to export from crystal report to pdf Pin
Eddy Vluggen1-Apr-16 1:36
professionalEddy Vluggen1-Apr-16 1:36 
GeneralRe: Font error when using C# code behind to export from crystal report to pdf Pin
botngot833-Apr-16 22:05
botngot833-Apr-16 22:05 
QuestionIssue around converting Expression<Func<T, bool>> to Expression<Func<U, bool>> Pin
RichardGrimmer31-Mar-16 3:36
RichardGrimmer31-Mar-16 3:36 
AnswerRe: Issue around converting Expression<Func<T, bool>> to Expression<Func<U, bool>> Pin
Richard Deeming31-Mar-16 4:59
mveRichard Deeming31-Mar-16 4:59 
This sort of expression rewriting can get very complicated. Something like this should work for fairly simple expressions:
C#
public sealed class ExpressionParameterModificationVisitor<TFrom, TTo> : ExpressionVisitor
{
    private readonly Stack<Dictionary<ParameterExpression, ParameterExpression>> _parameterMaps = new Stack<Dictionary<ParameterExpression, ParameterExpression>>();
    
    protected override Expression VisitLambda<T>(Expression<T> node)
    {
        var parameters = VisitAndConvert<ParameterExpression>(node.Parameters, "VisitLambda");
        var parameterMap = node.Parameters.Zip(parameters, (source, dest) => new { source, dest }).ToDictionary(p => p.source, p => p.dest);
        _parameterMaps.Push(parameterMap);
        try
        {
            var body = Visit(node.Body);
            return Expression.Lambda(body, parameters);
        }
        finally
        {
            _parameterMaps.Pop();
        }
    }
    
    protected override Expression VisitParameter(ParameterExpression expression)
    {
        if (typeof(TFrom) != expression.Type) return expression;
        return Expression.Parameter(typeof(TTo), expression.Name);
    }
    
    private Expression MapParameter(Expression expression)
    {
        var param = expression as ParameterExpression;
        if (param != null)
        {
            ParameterExpression result;
            if (_parameterMaps.Count != 0 && _parameterMaps.Peek().TryGetValue(param, out result))
            {
                expression = result;
            }
        }
        
        return expression;
    }
    
    protected override Expression VisitMember(MemberExpression node)
    {
        if (typeof(TFrom) != node.Expression.Type) return node.Update(Visit(node.Expression));
        
        var expression = MapParameter(node.Expression);
        var member = typeof(TTo).GetProperty(node.Member.Name);
        return Expression.MakeMemberAccess(expression, member);
    }
    
    public static Expression<Func<TTo, TResult>> Modify<TResult>(Expression<Func<TFrom, TResult>> expression)
    {
        if (expression == null) throw new ArgumentNullException(nameof(expression));
        
        var visitor = new ExpressionParameterModificationVisitor<TFrom, TTo>();
        var result = visitor.Visit(expression);
        return (Expression<Func<TTo, TResult>>)result;
    }
}

NB: You need to maintain a stack of parameter maps to cope with nested lambdas - for example:
C#
foo => foo.SomeCollection.Count(bar => bar.SomeFlag)




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer



modified 31-Mar-16 11:48am.

GeneralRe: Issue around converting Expression<Func<T, bool>> to Expression<Func<U, bool>> Pin
Sascha Lefèvre31-Mar-16 5:21
professionalSascha Lefèvre31-Mar-16 5:21 
GeneralRe: Issue around converting Expression<Func<T, bool>> to Expression<Func<U, bool>> Pin
Richard Deeming31-Mar-16 5:49
mveRichard Deeming31-Mar-16 5:49 
GeneralRe: Issue around converting Expression<Func<T, bool>> to Expression<Func<U, bool>> Pin
Sascha Lefèvre31-Mar-16 6:41
professionalSascha Lefèvre31-Mar-16 6:41 
QuestionUsing Deployment Item with Directory.GetFiles Pin
RamboSp31-Mar-16 2:17
RamboSp31-Mar-16 2:17 
AnswerRe: Using Deployment Item with Directory.GetFiles Pin
Eddy Vluggen1-Apr-16 1:33
professionalEddy Vluggen1-Apr-16 1:33 
Questioncurrent state of old issue of Enum boxing when used as Keys in a Dictionary ? Pin
BillWoodruff31-Mar-16 2:15
professionalBillWoodruff31-Mar-16 2:15 
AnswerRe: current state of old issue of Enum boxing when used as Keys in a Dictionary ? Pin
Richard Deeming31-Mar-16 2:52
mveRichard Deeming31-Mar-16 2:52 
QuestionMessage Removed Pin
31-Mar-16 0:25
PomegranateTiger31-Mar-16 0:25 
QuestionSync SQLite with Oracle or Postgre Pin
Potestas30-Mar-16 21:42
Potestas30-Mar-16 21:42 
AnswerRe: Sync SQLite with Oracle or Postgre Pin
Nathan Minier31-Mar-16 1:26
professionalNathan Minier31-Mar-16 1:26 
QuestionC# Tic Tac Toe "Simulator" Pin
Member 1242680830-Mar-16 11:24
Member 1242680830-Mar-16 11:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.