MVC 助手扩展问题

发布于 2024-09-05 23:45:44 字数 445 浏览 3 评论 0原文

我需要在我的 MVC 项目中实现一个 HtmlHelper 扩展,只是为了输出一些字符串,但仅在调试模式下,而不是在发布模式下。 我的第一次尝试是:

[Conditional("DEBUG")]
public static string TestStringForDebugOnly(this HtmlHelper helper, string testString)
{
    return testString;
}

但显然这会产生编译错误:

“条件属性无效,因为它的返回类型不是 void。”

所以我的理解是,一旦设置了 [Conditional] 属性,它就不允许返回任何内容?为什么?

还有其他方法可以实现这种功能吗?任何帮助将不胜感激。

I need to implement a HtmlHelper extension in my MVC project simply just to output some string but ONLY in the DEBUG mode, not in RELEASE.
My first attempt would be:

[Conditional("DEBUG")]
public static string TestStringForDebugOnly(this HtmlHelper helper, string testString)
{
    return testString;
}

But obviously that would give a compile error:

"The Conditional attribute is not valid because its return type is not void."

So my understanding is once you set the [Conditional] attribute, it doesn't allow anything to be returned? Why?

Is there another way to implement this kind of function? Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-09-12 23:45:45

我不知道最初的设计师最初是否有这样的想法,但这对我来说是有意义的。

因为您只能将该方法声明为 void 类型,并且它不能被覆盖,也不能作为接口的一部分进行引用,所以编译器可以简单地忽略该方法,因为如果指定的模式不匹配,它将没有依赖项。如果另一个方法以非匹配模式调用它,编译器可以简单地将其视为无效方法调用,就好像该属性不存在一样。

约翰的示例可以工作,但我会这样做:

#if DEBUG
public static string TestStringForDebugOnly(...)
{
    ...
}
#endif

// Arguments are only for illustration.
public string CallingMethod(int id, string temp)
{

    #if DEBUG
    string result = TestStringForDebugOnly(id, temp);
    #else
    string result = TestString(id, temp);
    #endif

    return result;
}

出于两个原因,我会花费额外的精力将所有与调试相关的代码(包括各个方法调用)包装在预处理器指令中。首先,它提供了自己的内置文档;您确切地知道什么应该在何时、何地以及为什么运行。其次,如果需要修改或删除代码,则需要进行的搜索量会大大减少,并且需要反复编译和重新编译以查看哪些内容损坏。

I don't know if the original designers had this line of thinking in mind originally, but this is how it makes sense to me.

Because you can only declare the method as type void AND it cannot be overridden AND it cannot be referenced as part of an interface, the compiler can simply ignore the method because it will have no dependencies if the specified mode doesn't match. If another method calls it in a non-matching mode, the compiler can simply treat it an invalid method call as if the attribute wasn't there.

John's example will work, but I would do something like this:

#if DEBUG
public static string TestStringForDebugOnly(...)
{
    ...
}
#endif

// Arguments are only for illustration.
public string CallingMethod(int id, string temp)
{

    #if DEBUG
    string result = TestStringForDebugOnly(id, temp);
    #else
    string result = TestString(id, temp);
    #endif

    return result;
}

I would expend the additional effort to wrap up all the DEBUG-related code (including the individual method calls) in preprocessor directives for two reasons. First, it provides its own built-in documentation; you know exactly what is supposed to run when, where and why. Second, if the code needs to be modified or removed, the amount of searching that needs to be done is greatly reduced, along with the need to repeatedly compile and re-compile to see what breaks.

波浪屿的海角声 2024-09-12 23:45:44

您可以使用 预处理器指令

public static string TestStringForDebugOnly(this HtmlHelper helper, string testString)
{
#if DEBUG
    return "debug";
#else
    return "other";
#endif
}

就您最初的问题而言要了解原因,请查看C# 规范的第 17.4.2 节表示:

[A] 条件方法的返回类型必须为 void

我只能推测为什么该语言的设计者会做出这样的决定,但我大胆猜测这是因为 C# 编译器不编译该方法如果条件为 false,则调用 IL,因此实际上就好像您从未调用过该方法(如果需要返回值,这会在运行时导致一些明显的问题!)

You can use a preprocessor directive:

public static string TestStringForDebugOnly(this HtmlHelper helper, string testString)
{
#if DEBUG
    return "debug";
#else
    return "other";
#endif
}

As far as your original question as to why, a peek at section 17.4.2 of the C# specification indicates:

[A] conditional method must have a return type of void

I can only speculate as to why the designers of the language decided this, but I would venture to guess it's because the C# compiler does not compile the method call into IL if the condition is false, so in effect it's as if you never called the method (which would cause some obvious problems at run-time if a return value was expected!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文