问题描述
请按照从Error用显式转换时使用CollectAs<>
从的WebMethod code
Code from WebMethod
return client.Cypher
.Match("(person:Person)")
.Where((Person person) => person.Email == username)
.OptionalMatch("(person)-[:SPEAKS]-(language:Language)")
.OptionalMatch("(person)-[:CURRENT_LOCATION]-(country:Country)"
.Return((person, language, country) => new ProfileObject
{
Person = person.As<Person>(),
Language = language.CollectAs<Language>(),
Country = country.CollectAs<Country>()
}).Results.ToList();
从国家类code:
Code from Country Class:
public class Language
{
public string Name { get; set; }
}
从ProfileObject类新的code:
New code from ProfileObject Class:
public class ProfileObject
{
public Person Person { get; set; }
public IEnumerable<Node<Language>> Language { get; set; }
public IEnumerable<Node<Country>> Country { get; set; }
}
此错误只有当我设置ProfileObject返回的IEnumerable>,如果我回到它发生在刚
This error only happens when I set ProfileObject to return IEnumerable>, if I return it to just
public Country Country {get; set;}
然后它工作(但我明显地得到重复条目的人,每个国家对象返回。
then it works (but I obviously get duplicated Person entries for each Country object returned.
任何人能告诉我解决这个问题,不涉及我翻录所有code出来并重新开始?
Anyone able to show me a solution to this problem that doesn't involve me ripping all of the code out and starting again?
编辑,包括堆栈跟踪:
[出现InvalidOperationException:Neo4jClient.Node`1 [Graph.Language]不能序列化,因为它没有参数的构造函数]
[InvalidOperationException: Neo4jClient.Node`1[Graph.Language] cannot be serialized because it does not have a parameterless constructor.]
[出现InvalidOperationException:无法序列成员Graph.ProfileObject.Language类型System.Collections.Generic.List 1 [Neo4jClient.Node
1 [图表。语言,图形,版本= 1.0.0.0,文化=中立,公钥=空],Neo4jClient,版本= 0.0.0.0,文化=中立,公钥=空],请参见内部异常的更多细节。]
System.Xml.Serialization.StructModel.CheckSupportedMember(TYPEDESC TYPEDESC,成员的MemberInfo,型型)5451673
System.Xml.Serialization.StructModel.CheckSupportedMember(TYPEDESC TYPEDESC,成员的MemberInfo,型型)+69
System.Xml.Serialization.StructModel.GetPropertyModel(的PropertyInfo的PropertyInfo)+125
System.Xml.Serialization.StructModel.GetFieldModel(的MemberInfo的MemberInfo)+89
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping映射,StructModel模型,布尔openModel,字符串的typeName,RecursionLimiter限制器)618
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel模型,NS字符串,布尔openModel,XMLATTRIBUTES一,RecursionLimiter限制器)+378
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,NS字符串,ImportContext背景下,string数据类型,XMLATTRIBUTES一,布尔重复,布尔openModel,RecursionLimiter限制器)1799
[InvalidOperationException: Cannot serialize member 'Graph.ProfileObject.Language' of type 'System.Collections.Generic.List1[[Neo4jClient.Node
1[[Graph.Language, Graph, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Neo4jClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.] System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +5451673 System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +69 System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +125 System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) +89 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +618 System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +378 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1799
[InvalidOperationException异常:有反映类型'Graph.ProfileObject'错误]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,NS字符串,ImportContext背景下,string数据类型,XMLATTRIBUTES一,布尔重复,布尔openModel,RecursionLimiter限制器)1917
System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping,XmlArrayItemAttributes属性,类型arrayElementType,字符串arrayElementNs,RecursionLimiter限制器)+263
System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel模型,NS字符串,RecursionLimiter限制器)+264
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问,FieldModel模型,XMLATTRIBUTES一,字符串NS,类型choiceIdentifierType,布尔RPC,布尔openModel,RecursionLimiter限制器)5456308
System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember,字符串NS,XmlReflectionMember [] xmlReflectionMembers,布尔RPC,布尔openModel,RecursionLimiter限制器)+852
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember [] xmlReflectionMembers,字符串NS,布尔hasWrapperElement,布尔RPC,布尔openModel,RecursionLimiter限制器)286
[InvalidOperationException: There was an error reflecting type 'Graph.ProfileObject'.] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1917 System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, String arrayElementNs, RecursionLimiter limiter) +263 System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter) +264 System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +5456308 System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +852 System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +286
[InvalidOperationException异常:有反映MyResult错误]
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember [] xmlReflectionMembers,字符串NS,布尔hasWrapperElement,布尔RPC,布尔openModel,RecursionLimiter限制器)979
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(字符串的ElementName,字符串NS,XmlReflectionMember []的成员,布尔hasWrapperElement,布尔RPC,布尔openModel,XmlMappingAccess访问)+133
System.Web.Services.Protocols.Soa preflector.ImportMembersMapping(XmlReflectionImporter xmlImporter,健胃preflectionImporter soapImporter,布尔serviceDefaultIsEn codeD,布尔RPC,SoapBindingUse使用,SoapParameterStyle paramStyle,字符串的ElementName,字符串elementNamespace,布尔nsIsDefault ,XmlReflectionMember []的成员,布尔验证,布尔openModel,串键,布尔WRITEACCESS)+240
System.Web.Services.Protocols.Soa preflector.ReflectMethod(LogicalMethodInfo的MethodInfo,布尔客户端,XmlReflectionImporter xmlImporter,健胃preflectionImporter soapImporter,字符串defaultNs)2893
[InvalidOperationException: There was an error reflecting 'MyResult'.] System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +979 System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access) +133 System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, Boolean serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members, Boolean validate, Boolean openModel, String key, Boolean writeAccess) +240 System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +2893
[出现InvalidOperationException:方法ProfileServices.My无法体现]
System.Web.Services.Protocols.Soa preflector.ReflectMethod(LogicalMethodInfo的MethodInfo,布尔客户端,XmlReflectionImporter xmlImporter,健胃preflectionImporter soapImporter,字符串defaultNs)6173
System.Web.Services.Description.SoapProtocolReflector.ReflectMethod()+137
System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding)1776
System.Web.Services.Description.ProtocolReflector.Reflect()641
System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector []反射器)+685
System.Web.Services.Description.ServiceDescriptionReflector.Reflect(类型类型,字符串URL)+118
System.Web.Services.Protocols.DocumentationServerType..ctor(类型类型,字符串的uri,布尔excludeSchemeHostPortFromCachingKey)+230
System.Web.Services.Protocols.DocumentationServerProtocol.Initialize()434
System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型,HttpContext的背景下,Htt的prequest请求的Htt presponse响应,布尔和放大器; abortProcessing)+122
[InvalidOperationException: Method ProfileServices.My can not be reflected.] System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +6173 System.Web.Services.Description.SoapProtocolReflector.ReflectMethod() +137 System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding) +1776 System.Web.Services.Description.ProtocolReflector.Reflect() +641 System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[] reflectors) +685 System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url) +118 System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri, Boolean excludeSchemeHostPortFromCachingKey) +230 System.Web.Services.Protocols.DocumentationServerProtocol.Initialize() +434 System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +122
[出现InvalidOperationException:无法处理请求]
System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型,HttpContext的背景下,Htt的prequest请求的Htt presponse响应,布尔和放大器; abortProcessing)+ 320
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(类型类型,HttpContext的背景下,Htt的prequest请求的Htt presponse响应)+171
[InvalidOperationException: Unable to handle request.] System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +320 System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +171
[出现InvalidOperationException:无法处理请求]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(类型类型,HttpContext的背景下,Htt的prequest请求的Htt $ P psponse响应$)+374
System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext的背景下,动词的字符串,字符串URL,文件路径字符串)+ 209
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext的背景下,字符串请求类型,URL字符串,字符串pathTranslated)+48
System.Web.HttpApplication.MapHttpHandler(HttpContext的背景下,字符串的RequestType,VirtualPath路径,字符串pathTranslated,布尔useAppConfig)+226
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+145
System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器; completedSynchronously)+155
[InvalidOperationException: Failed to handle request.] System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +374 System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +209 System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +48 System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +226 System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +145 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
推荐答案
默认情况下,的使用 的DataContractSerializer
序列化到XML,但错误信息不能序列成员&LT; X&GT;因为它是接口
是的XmlSerializer 生成,所以显然你已经切换到了。
By default, asp.net uses DataContractSerializer
to serialize to XML, however the error message "Cannot serialize member <x> because it is an interface"
is generated by XmlSerializer
, so apparently you have switched to that.
按规定你可以考虑切换回的DataContractSerializer
,可序列化类型的属性的IEnumerable&LT; T&GT;
只要底层类型 T
可序列化。
You could consider switching back to DataContractSerializer
as specified here, which can serialize properties of type IEnumerable<T>
as long as the underlying type T
can be serialized.
另外,如果你不想这样做,你可以修改你的 ProfileObject
类返回代理阵列进行序列化,而无需更改底层设计:
Alternatively, if you don't want to do that, you could modify your ProfileObject
class to return proxy arrays for serialization without changing your underlying design:
public class ProfileObject
{
public Person Person { get; set; }
[XmlIgnore]
public IEnumerable<Node<Language>> Language { get; set; }
[XmlIgnore]
public IEnumerable<Node<Country>> Country { get; set; }
[XmlArray("Languages")]
[XmlArrayItem("Language")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Node<Language>[] LanguageArray
{
get
{
if (Language == null)
return null;
return Language.ToArray();
}
set
{
Language = value;
}
}
[XmlArray("Countries")]
[XmlArrayItem("Country")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Node<Country>[] CountryArray
{
get
{
if (Country == null)
return null;
return Country.ToArray();
}
set
{
Country = value;
}
}
}
更新
的XmlSerializer
只会序列化的公共 GET
和设置$ C $性能C>方法。由于
Node.Data code>是GET-而已,它不能由
的XmlSerializer
连载。
XmlSerializer
will only serialize properties with public get
and set
methods. Since Node.Data
is get-only, it can't be serialized by XmlSerializer
.
由于您只需要序列化数据,而不是节点&lt; TDATA&GT;
,并且永远不需要反序列化,你可以使用LINQ返回改变你的节点枚举到数据序列化数组如下:
Since you only need to serialize the data and not the Node<TData>
, and never need to deserialize, you can use linq to return transform your enumerable of nodes to an array of data for serialization as follows:
public static class NodeExtensions
{
public static TData [] ToDataArray<TData>(this IEnumerable<Node<TData>> nodes)
{
if (nodes == null)
return null;
return nodes.Select(n => n.Data).ToArray();
}
}
public class ProfileObject
{
public Person Person { get; set; }
[XmlIgnore]
public IEnumerable<Node<Language>> Language { get; set; }
[XmlIgnore]
public IEnumerable<Node<Country>> Country { get; set; }
[XmlArray("ArrayOfLanguage")]
[XmlArrayItem("Language")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Language [] LanguageArray
{
get
{
return Language.ToDataArray();
}
set
{
throw new NotImplementedException();
}
}
[XmlArray("ArrayOfCountry")]
[XmlArrayItem("Country")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Country [] CountryArray
{
get
{
return Country.ToDataArray();
}
set
{
throw new NotImplementedException();
}
}
}
这篇关于无法序列成员&LT; X&GT;因为它是一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!