用于序列化 IEnumerable<T> 的扩展方法对象到字符串数组? [英] Extension Method for serializing an IEnumerable&lt;T&gt; object to a string array?

查看:19
本文介绍了用于序列化 IEnumerable<T> 的扩展方法对象到字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最后一个问题是关于获取序列化为 XML 的对象的字符串表示.其中一个响应者写了一个扩展方法来封装序列化过程.

My last question was about getting the string representation of an object serialized to XML. One of the responders wrote an extension method to encapsulate the serialization process.

我现在的问题是如何使用扩展方法返回一个字符串数组,当传递一个 IEnumerable 对象时,数组中的每个 string将是 IEnumerable 的单独序列化元素.

My question now is how can I use an Extension Method to return an array of strings, when passed an IEnumerable<T> object, where each string in the array would be an individually serialized element of the IEnumerable<T>.

提前致谢!

推荐答案

使用链接中的代码示例,您可以添加另一个这样的方法或使用 ToArray() 方法将它们转换为数组.

using the code example from your link, you could add another method like this or convert them into array using ToArray() method.

public static class XmlTools
{
  public static IEnumerable<string> ToXmlString<T>(this IEnumerable<T> inputs)
  {
     return inputs.Select(pArg => pArg.ToXmlString());
  }
}

这篇关于用于序列化 IEnumerable<T> 的扩展方法对象到字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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