【问题标题】:Can the C# preprocessor look at constant string values?C# 预处理器可以查看常量字符串值吗?
【发布时间】:2014-10-11 17:50:19
【问题描述】:

我正在为我正在辅导的孩子编写一个 MadLib 库。 MadLib 类具有以下内容:

// Finds fields
private static readonly Regex PromptValueRegex = new Regex("<([^>]+)>", RegexOptions.IgnoreCase);

// text of our story
private readonly string Story;

...

/// <summary>
/// Creates a new MadLib object with the given story text.
/// </summary>
/// <param name="story">The text of the story with blank field names denoted by &lt;blank name&gt;.</param>
public MadLib(string story)
{
    if (story == null)
    {
        throw new ArgumentNullException("Story cannot be null.");
    }

    if (story.Trim() == string.Empty)
    {
        throw new EmptyStoryException();
    }

    Story = story;

    SetupPromptDictionary();
}

该故事通过正则表达式 &lt;([^&gt;]+)&gt; 查找 "&lt;blank name&gt;" 来检测过去 MadLibs 中的空白。

如果可以确定传递给构造函数的故事没有&lt;&gt; 字符,我想发出编译时警告。这可能吗?

【问题讨论】:

  • 你想如何对 runtime 中确定的内容发出 compile time 警告?
  • 如果他使用new MadLib("my story")之类的东西,因为在MadLib对象初始化后无法设置故事,那么肯定MadLib对象值得警告。
  • new MadLib("my story") 是运行时代码,警告是编译时。
  • 但是"my story" 是一个常数。常量可以通过编译时间来确定。
  • 不过,这不是编译器错误/警告。它与他使用 new MadLib(null) 之类的东西属于同一类

标签: c# c-preprocessor


【解决方案1】:

通过 cmets,这是不可能的。如果有可能,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多