2

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...

Community
  • 1
  • 1
Budda
  • 18,015
  • 33
  • 124
  • 206

4 Answers4

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.

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • I've tried that. Doing that manually VS told that dll is already referenced (but I don't see how). In order to add it through the web,config - I need public token... Could you advise, how can I get it? – Budda Jan 13 '11 at 14:48
1

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>
hunter
  • 62,308
  • 19
  • 113
  • 113
1

I don't know what is a source of such strange behavior... everything is fine with this Extension Method... answer for another question resolved current question too.

codeMonkey
  • 4,134
  • 2
  • 31
  • 50
Budda
  • 18,015
  • 33
  • 124
  • 206
0

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.

codeMonkey
  • 4,134
  • 2
  • 31
  • 50