扩展方法存在问题:IXmlLineInfo
当我尝试在 ascx-control 中的类中使用任何扩展方法时:
<%@ Import Namespace="VfmElita.Page.Stat" %>
<%=new MyTestClass().ExtMethod() %>
这是世界上最简单的方法:(
namespace VfmElita.Page.Stat
{
public static class TestExtention
{
public static string ExtMethod(this MyTestClass test)
{
return "Hope for result";
}
}
}
它位于控件的 ascx.cs 文件中,
我收到以下错误:
错误 CS0012:类型“System.Xml.IXmlLineInfo”是在未引用的程序集中定义的。您必须添加对程序集“System.Xml,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e”的引用。
如果我用任何属性替换 ExtMethod()
<%= Team.GetTeamById(2).PropOk %>
例如,
,一切都很好......为什么?我怎样才能防止这种情况发生?
PS 问题似乎与我之前的或另一个。但当前的一项更为具体且相当详细。
PS 我尝试手动添加对网站的引用,VisualStuido 告诉它已经有引用了......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您的项目没有对 System.Xml 的引用,并且您在扩展方法的实际实现中使用它。
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.确保您在控制标头中导入扩展方法的命名空间:
或者按照我的喜好,将其添加到 web.config 中,这样您就不必到处导入
make sure you're either importing the namespace of your extension method in the control header:
or my preference, adding it to the web.config so you don't have to import all over the place
我不知道这种奇怪行为的根源是什么...这个扩展方法一切都很好...答案 另一个问题也解决了当前问题。
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.
我真的很喜欢这个答案:
您可以在CodeBehind中的函数中执行扩展。
您可以使用
您的 CodeBehind 将具有:
不要忘记导入扩展模块的名称空间。
I really like this answer:
You can perform the extension in a function in CodeBehind.
You can use
Your CodeBehind will have:
Don't forget to import the namespace of your extension module.