MONO - .NET跨平台之亲身经历(一)

  下午在 RedHat Linux 9/MONO 1.0 中编译了在以往 .net 项目中已经使用过的两个类库项目,其中第一个较简单的项目顺利编译通过,在第二个XML类库项目中出现编译异常,而这个项目曾经经过微软的CSC编译器通过的啊,mono-mcs编译器的反馈信息如下:

XMLUtility.cs(73) error CS1502: The best overloaded match for method 'System.Xml.XmlAttribute System.Xml.XmlAttributeCollection.Remove(System.Xml.XmlAttribute)' has some invalid arguments.
XMLUtility.cs(73) error CS1503: Argument 0: Cannot convert from 'System.Xml.XmlNode' to 'System.Xml.XmlAttribute'.
XMLUtility.cs(73) error CS1501: No overload for method 'Remove' takes '1' arguments.
XMLUtility.cs(73) error CS8006: Could not find any applicable function for this argument list.
Compilation failed: 4 error(s), 0 warnings.

  打开偶的源代码,定位到出错提示位置,其上下文代码如下:
  提示:ownerNode 为函数的传入参数,其类型为System.Xml.XmlNode,attributeName 为函数的传入参数,其类型为String。

System.Xml.XmlNode attribute = ownerNode.Attribute.GetNamedItem(attributeName);

if(attribute != null)
 ownerNode.Attributes.Remove(attribute);

  将最后一句代码改为:ownerNode.Attributes.Remove((System.Xml.Attribute)attribute); 后编译成功!


  为什么在VS.net中以上代码可以顺利编译通过,而在MONO的mcs编译器中却无法编译通过呢?OK,先让我们来看看mcs的提示信息吧,大意是 在XmlAttributeCollection中没有找到与之调用相匹配的Remove的重载方法。
  通过VS.net的对象浏览器分别查看微软的System.Xml.dll和Mono的System.Xml.dll库文件,其定义均一样,如下:
public class XmlAttribute : System.Xml.XmlNode
public class XmlAttributeCollection : System.Xml.XmlNamedNodeMap

public virtual System.Xml.XmlAttributeCollection Attributes [get]
public virtual System.Xml.XmlAttribute System.Xml.XmlAttributeCollection.Remove(System.Xml.XmlAttribute node)
public virtual System.Xml.XmlNode System.Xml.XmlNamedNodeMap.GetNamedItem(string name)

  在 XmlAttributeCollection 类中只有一个唯一的 Remove 方法,也无重写(override)的重载。那么最后那行代码应该是编译不过才对啊,为什么在VS.net中又能通过呢?而且上面的.GetNamedItem(attributeName)调用返回的也只是 XmlNode 类型,因此编译器无法就此而做出智能判断,并默认进行转换啊。唉,搞不懂CSC是如何处理的? :-(
  最后,在 Linux 中用mcs编译出来的这个类库大小是43KB,而在 Win2000 中用 VS.net(csc)(Debug/Release版)编译出来的类库却有56KB。 :-(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值