【发布时间】: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 <blank name>.</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();
}
该故事通过正则表达式 <([^>]+)> 查找 "<blank name>" 来检测过去 MadLibs 中的空白。
如果可以确定传递给构造函数的故事没有< 或> 字符,我想发出编译时警告。这可能吗?
【问题讨论】:
-
你想如何对 runtime 中确定的内容发出 compile time 警告?
-
如果他使用
new MadLib("my story")之类的东西,因为在MadLib对象初始化后无法设置故事,那么肯定MadLib对象值得警告。 -
new MadLib("my story") 是运行时代码,警告是编译时。
-
但是
"my story"是一个常数。常量可以通过编译时间来确定。 -
不过,这不是编译器错误/警告。它与他使用
new MadLib(null)之类的东西属于同一类
标签: c# c-preprocessor