简体   繁体   中英

Problem with Extension method: IXmlLineInfo

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?

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

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

听起来您的项目没有对System.Xml的引用,并且您在扩展方法的实际实现中使用它。

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>

我不知道这种奇怪行为的根源是什么......这个扩展方法一切都很好...... 另一个问题的答案也解决了当前问题。

I really like this answer :

You can perform the extension in a function in CodeBehind.

You can use

<%# ExtMethod((MyTestClass)Container.DataItem) %>

Your CodeBehind will have:

protected string ExtMethod(MyTestClass test)
{
    return test.ExtMethod();
}

Don't forget to import the namespace of your extension module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM