【发布时间】:2015-05-05 00:19:42
【问题描述】:
我正在尝试扩展来自已编译 DLL 的类方法,并尝试从 MVC 项目内部执行此操作。但是,由于某种原因,它没有接收到这个。
这就是我通常扩展实例化类方法的方式,例如:
public static string CSVEncode(this string original)
{
//.......
}
有了这个,如果我有任何字符串,我可以从对象本身看到并调用CSVEncode() 方法,例如:
string temp = "some string goes here";
string csv = temp.CSVEncode();
但是,在最近的一次尝试中,它根本不起作用......
为了这个问题的目的,这是我试图扩展的对象的一个小定义(例如:这里有更多不需要迭代的属性和方法)。
namespace SomeOtherDLL.HumanRace
{
public class Human
{
//... some properties...
public Human(){ }
public bool CanAccess(string AppName)
{
//....
}
}
}
在我的 MVC 解决方案中,我有一个 Common 项目,其中包括一个名为 Extensions 的类。在这个类中,我放置了所有扩展,包括我试图为上面的对象执行的扩展。
但是,这在之后的任何地方都不会出现在 Intellisense 中,如果我尝试构建或编译,我会收到一条错误消息,指出此方法不存在,我就是不明白为什么?
namespace MyProj.Common
{
public static class Extensions
{
public static bool CanAccess(this HumanRace.Human original, int AppID)
{
//...some code here...
}
}
}
现在从我过去做过的其他对象扩展来看,这应该可以完美地工作......
这是我如何尝试在视图页面中使用它的示例:
@model SomeOtherDLL.HumanRace.Human
@using MyProj.Common.Extensions
@Html.Raw(Model.CanAccess(59) ? "<img src='CanAccess.jpg' />" : "<img src='CannotAccess.jpg' />")
.............
这在 Visual Studio 中无法解决...我在这里做错了吗?
【问题讨论】:
标签: c# asp.net-mvc-4