无法序列化类型 [英] can not serialize the type

查看:242
本文介绍了无法序列化类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有方法写作参数lambda

i调用方法:

i have the method write that take as a parameter a lambda
i call method :

Write(() => E01020003, userCred, "test", 1, "test");





方法名为





the method called

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









但是现在我遇到了问题,因为方法write是一种接口方法。因此,当我编译我的项目时,我的界面出现问题









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


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);
)





编译器说我:



你无法序列化类型'System.Linq.Expressions.Expression1 [System.Func1 [T]]'。尝试使用属性DataContractAttribute标记它,并使用属性DataMemberAttribute标记其所有成员的序列化。如果类型是集合,请尝试使用属性CollectionDataContractAttribute标记它。对于支持的其他类型,请参阅Microsoft .NET Framework的文档。



那么我该如何解决这个问题?

谢谢!



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!

推荐答案

默认情况下,表达式树不可序列化。为了完成这项工作,你必须跳过一些额外的箍:



有自定义解决方案来序列化表达式树。我知道Interlinq可以使用任意表达式树(我自己使用它):

http://interlinq.codeplex.com / [ ^ ]



我认为这个解决方案也是如此:

http://expressiontree.codeplex.com/ [ ^ ]



这也可能,但我没有仔细看看它:

http:// metalinq.codeplex.com/ [ ^ ]

http://reverseblade.blogspot.de/2008/11/how -to-serialize-lambda-expressions.html [ ^ ](使用MetaLinq的示例)



虽然如果你的lambda表达式总是像你的问题一样简单,比如
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
() => E01020003

。 。(如果E01020003是可序列化类型的话)那么开发你自己的专用解决方案可能是最简单的(在这种情况下,只需直接传递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).


你确定你想要将可执行代码作为参数传递给您的服务吗?你认为它是安全的吗?



此外,Lambda表达式不可序列化。你需要考虑另一种方法。如果你能解释一下你想要实现的目标,那么我们可以帮你找到另一种方法来实现它..
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..


我找到解决方案



i创建一个扩展方法,所以我可以这样打电话:



例如:



i find the solution

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

for example:

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







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 "";
        }
    }





所以返回s.Name.Substring(1).ToString();给我012345

并返回s.Name.ToString();给我E012345



so return s.Name.Substring(1).ToString(); give me 012345
and return s.Name.ToString(); give me E012345


这篇关于无法序列化类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆