Problem with Extension method: IXmlLineInfo

女生的网名这么多〃 提交于 2019-12-11 06:49:02

问题


When I try to use any extension method to my class in ascx-control:

<%@ Import Namespace="VfmElita.Page.Stat" %>
<%=new MyTestClass().ExtMethod() %>

and here is the simplest method in the world:

namespace VfmElita.Page.Stat
{
public static class TestExtention
{
    public static string ExtMethod(this MyTestClass test)
    {
        return "Hope for result";
    }
}
}

(it is located in ascx.cs-file of the control

I got the following error:

error CS0012: The type 'System.Xml.IXmlLineInfo' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.

If I replaced ExtMethod() with any property

<%= Team.GetTeamById(2).PropOk %>

for example, everything is fine...

Why? How can I prevent this?

P.S. It seems like question is duplicate to one of my previous or another one. But the current one is more specific and pretty detailed.

P.S. I've tried to add reference to web-site manually, VisualStuido tells that it has reference already...


回答1:


It sounds like your project doesn't have a reference to System.Xml and you are using it within the real implementation of your extension method.




回答2:


make sure you're either importing the namespace of your extension method in the control header:

<%@ Import Namespace="My.Extension.Namespace" %>

or my preference, adding it to the web.config so you don't have to import all over the place

<pages>
    <namespaces>
        <add namespace="My.Extension.Namespace"/>
    </namespaces>
</pages>



回答3:


I don't know what is a source of such strange behavior... everything is fine with this Extension Method... answer for another question (http://stackoverflow.com/questions/4665475/problem-with-system-xml-ixmllineinfo/4719876#4719876) resolved current question too.



来源:https://stackoverflow.com/questions/4680780/problem-with-extension-method-ixmllineinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!