c#中引用类型做参数时的赋值陷阱

最近在工作中遇到一个使用XDocument的情况,以前在C# 2.0中使用XmlDocument时,将Xml 字符串载入一个XmlDocument对象时只需要直接调用XmlDocument对象的LoadXml方法,而在使用XDocument的时候必须调用XDocument的静态方法: <pre lang="csharp"> XDocument doc = new XDocument(); doc = XDocument.Parse(someXmlString); </pre> 这样写起来不太符合面向对象的原则。于是就想通过扩展方法来实现XDocument对象的LoadXml方法: <pre lang="csharp"> public static class XmlExtensions { public static bool LoadXml(this XDocument d, string xml) { try { d = XDocument.Parse(xml); return true; } catch …

继续阅读 »

posted @ 2011-04-11 13:56  KunZhu  阅读(378)  评论(0编辑  收藏  举报