有关反序列化的SVC服务错误 [英] SVC Service Error regarding Deserializing
本文介绍了有关反序列化的SVC服务错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从svc服务获取目录的子目录列表,但它显示错误 -
类型System.IO.FileSystemInfo无法反序列化,因为它没有参数少的公共构造函数。
请帮助我解决这种情况。
代码片段 -
Hi,
I am trying to get the list of sub-directories of a directory from svc service, but it shows an error --
The type System.IO.FileSystemInfo cannot be deserialized as it doesn''t has parameter less public constructor.
Please help me out from this situation.
Code Snippet--
[OperationContract]
private System.Collections.Generic.List<DirectoryInfo> GetSubDirectories(string directoryPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(directoryPath);
DirectoryInfo[] dirs = dir.GetDirectories();
System.Collections.Generic.List<DirectoryInfo> childs = new System.Collections.Generic.List<DirectoryInfo>();
foreach (DirectoryInfo dir1 in dirs)
{
// XmlObjectSerializer xml_serial = new XmlObjectSerializer(typeof(XML_directory));
childs.Add(dir1);
}
return childs;
}
catch (Exception) { return null; }
//return null;
}
推荐答案
我的第一个建议是尝试从directoryinfo类创建一个继承的对象。但它是密封的。无论如何,我不会建议你返回DirectoryInfo列表。我会创建一个DTO(数据传输对象)。请参阅以下内容:
DTO:
My first suggestion would have been to try to create an inherited object from the directoryinfo class. But it is sealed. Anyway I would not recomend to you to return with a list of DirectoryInfo. I would create a DTO (Data Transfer Object). See the following:
The DTO:
[DataContract]
public Class DirectoryInfoDTO
{
[DataMember]
public DateTime CreationTime {get ; set;}
[DataMember]
public string Name {get ; set;}
}
< u>然后是代码: // 包括:System.Linq;
And then the code: // Include: System.Linq;
var result = dirs.Select((a,b) => new DirectoryInfoDTO()
{
Name = a.FullName,
CreationTime = a.CreationTime
}
结果将是DirectoryInfoDTO的集合
the result will be a collection of DirectoryInfoDTO
这篇关于有关反序列化的SVC服务错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文