Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the method write that take as a parameter a lambda
i call method :
C#
Write(() => E01020003, userCred, "test", 1, "test");


the method called

C#
<pre lang="cs">public bool Write<T>(Expression<Func<T>> expr, UserCredentials userCredentials, string ErrorText, int type, string infoText){}




But now i have a problem, because the method write is an interface method. So when i compile my project i have problem in my interface


C#
FaultContract(typeof(MyFaultContract))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
bool Write<T>(
[MessageParameter(Name = "expr")] Expression <Func<T>>expr,
[MessageParameter(Name = "userCredential")] UserCredentials userCredential,
[MessageParameter(Name = "error")] string error,
[MessageParameter(Name = "type")] int type,
[MessageParameter(Name = "infoText")] string infoText);
)


the compiler say me:

You can not serialize the type 'System.Linq.Expressions.Expression1 [System.Func1 [T]]'. Try to mark it with the attribute DataContractAttribute and tag all of its members to be serialized with the attribute DataMemberAttribute. If the type is a collection, try to mark it with the attribute CollectionDataContractAttribute. For other types supported, see the documentation for Microsoft .NET Framework.

So how can i fix this problem?
thank you!
Posted
Updated 3-Jun-15 2:46am
v2

Expression-trees are not serializable by default. To make this work you have to jump through some extra hoops:

There are custom solutions to serialize expression-trees. I know that Interlinq works with arbitrary expression trees (I use it myself):
http://interlinq.codeplex.com/[^]

I assume that this solution does as well:
http://expressiontree.codeplex.com/[^]

And this probably too but I didn't take a closer look at it:
http://metalinq.codeplex.com/[^]
http://reverseblade.blogspot.de/2008/11/how-to-serialize-lambda-expressions.html[^] (Example for using MetaLinq)

Though if your lambda-expressions are always as simple as in your question, like
C#
() => E01020003
..(and if E01020003 is of a serializable type) then it might be easiest to develop your own, specialized solution (in this case, just passing E01020003 directly).
 
Share this answer
 
v2
Are you sure that you want executable code to be passed to your service as parameter? Do you think it will be secure?

Also, Lambda expressions are not serializable. You need to think of another approach. If you could explain what you are trying to achieve, then may be we can help you find another way to do it..
 
Share this answer
 
i find the solution

i create a extension method so i can call in this way:

for example:

C#
string E012345= "generic error";
string nameVar = E012345.getName();


C#
public static class Ext
    {
        public static string GetNumber(this string errorString)
        {
            var yyy = typeof(ErrorBase).GetProperties();
            foreach (var s in yyy)
            {
                PropertyInfo p = typeof(ErrorBase).GetProperty(s.Name);
                var m = p.GetValue(s);
                if (m != null)
                    if (p.GetValue(s).ToString().CompareTo(errorString) == 0)
                    {
                        return s.Name.Substring(1).ToString();
                    }
            }
            return "";
        }
    }


so return s.Name.Substring(1).ToString(); give me 012345
and return s.Name.ToString(); give me E012345
 
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