我得到的例外是:
MyNamespace.Program的类型初始值设定项引发了异常
内部异常说
消息:值不能为空。参数名称:集合
来源: ...哈希集......
这让我相信错误发生在下面指出的行上......
class Program
{
public static IEnumerable<char> WordCharacters = ExpandCharacterSet("A-Za-z0-9_");
public static IEnumerable<char> NonWordCharacters = ExpandCharacterSet("^A-Za-z0-9_");
public static IEnumerable<char> SpaceCharacters = ExpandCharacterSet(" \f\n\r\t\v");
public static IEnumerable<char> NonSpaceCharacters = ExpandCharacterSet("^ \f\n\r\t\v");
public static IEnumerable<char> DigitCharacters = ExpandCharacterSet("0-9");
public static IEnumerable<char> NonDigitCharacters = ExpandCharacterSet("^0-9");
public static IEnumerable<char> WildCharacters = ExpandCharacterSet("^\n");
public static IEnumerable<char> AllCharacters = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));
public static IEnumerable<char> ExpandCharacterSet(string set)
{
if (set.Length == 0)
return "";
var sb = new StringBuilder();
int start = 0;
bool invertSet = false;
if (set[0] == '[' && set[set.Length - 1] == ']')
set = set.Substring(1, set.Length - 2);
if (set[0] == '^')
{
invertSet = true;
set = set.Substring(1);
}
set = Regex.Unescape(set);
foreach (Match m in Regex.Matches(set, ".-.|."))
{
if (m.Value.Length == 1)
sb.Append(m.Value);
else
{
if (m.Value[0] > m.Value[2]) throw new ArgumentException("Invalid character set.");
for (char c = m.Value[0]; c <= m.Value[2]; ++c)
sb.Append(c);
}
}
if (!invertSet) return sb.ToString();
var A = new HashSet<char>(AllCharacters); // <---- change this to "ABC" and the error goes away
var B = new HashSet<char>(sb.ToString());
A.ExceptWith(B);
return A;
}
static void Main(string[] args)
{
}
}
但我不知道为什么。当我打印字符时,
Console.WriteLine(string.Concat(AllChars));
按预期打印每个字符。那么为什么HashSet认为它是空的呢?
答案 0 :(得分:5)
我猜测ExpandCharacterSet正用于初始化另一个静态字段。无法保证两个静态字段的初始化顺序,因此可能会在初始化AllChars之前尝试初始化另一个字段。
尝试使用正确的初始化顺序将赋值移动到显式静态构造函数中。
例如,如果您当前的代码有:
public static IEnumerable<char> AllChars = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));
public static IEnumerable<char> Expanded = ExpandCharacterSet(...);
改为:
public static IEnumerable<char> AllChars;
public static IEnumerable<char> Expanded;
static <ClassName> {
AllChars = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));
Expanded = ExpandCharacterSet(...);
}
答案 1 :(得分:0)
除了基于以下两行代码之外,我没有得到任何内容。抛出异常的代码行在哪里?
public static IEnumerable<char> AllChars = Enumerable.Range(0, 256)
.Select(Convert.ToChar)
.Where(c => !char.IsControl(c));
var A = new HashSet<char>(AllChars);
答案 2 :(得分:0)
我认为,AllChars从代码的其他部分设置为null,因为上述两种方法似乎工作正常。
您能否提供更多实施细节。