Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good morning.

i have a problem with my code in c#.

this below is the right code(i can take the var name end so have string varName="E01020003") :

C#
public void writeTest()
{
  varName = getVarName(() => E01020003); 
  ...
  ...
}

C#
public string VarName<T>(Expression<Func<T>> expr)
        {
            var body = ((MemberExpression)expr.Body);
            return body.Member.Name;
        }


these two methods are in the same file .cs


i would like to to this:
FILE1:
C#
public void Write()
{
    ...
    ...
    ...
    file2.Write(userCred, E01020018, () => E01020003, (int)Type.Info, "test");
}

FILE2:
public bool Write(UserCredentials userCredentials, string ErrorText, Expression<Func<T>> expr, int type, string infoText)
{
    bool returnValue = true;

    var body = ((MemberExpression)expr.Body);

    string varName = body.Member.Name;
    ...
    ...
    ...
}



is it possible? i have problem to pass a lambda like a parameter.
problem 1 in the call
C#
file2.Write(userCred, E01020018, () => E01020003, (int)Type.Info, "test");


and in the method called:
C#
Write(UserCredentials userCredentials, string ErrorText, Expression<Func<T>> expr, int type, string infoText)


Best regards
Posted
Comments
[no name] 31-May-15 10:10am    
Do you face any error? can you try with replacing Expression<Func<string>> expr ?
Maciej Los 31-May-15 16:07pm    
Have a look here: http://www.codeproject.com/Articles/114931/Understanding-Predicate-Delegates-in-C

You use a generic definition for Expression<Func<T>> expr but you didn't declare your method Write as generic. If expr should always return a string then just change it to Expression<Func<string>> expr, otherwise you have to change the method declaration to Write<T>(..).
 
Share this answer
 
see my solution here thank u all for help me
now it works:

can not serialize the type[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900