类型初始值设定项引发异常
这个类抛出异常。它没有显示确切的行号,但听起来好像它发生在静态构造函数中:
static class _selectors
{
public static string[] order = new[] { "ID", "NAME", "TAG" };
public static Dictionary<string, Regex> match = new Dictionary<string, Regex> {
{ "ID", new Regex(@"#((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "CLASS", new Regex(@"\.((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "NAME", new Regex(@"\[name=['""]*((?:[\w\u00c0-\uFFFF-]|\\.)+)['""]*\]") },
{ "ATTR", new Regex(@"\[\s*((?:[\w\u00c0-\uFFFF-]|\\.)+)\s*(?:(\S?=)\s*(['""]*)(.*?)\3|)\s*\]") },
{ "TAG", new Regex(@"^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)") },
{ "CHILD", new Regex(@":(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?") },
{ "POS", new Regex(@":(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)") },
{ "PSEUDO", new Regex(@":((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\((['""]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?") }
};
public static Dictionary<string, Action<HashSet<XmlNode>, string>> relative = new Dictionary<string, Action<HashSet<XmlNode>, string>> {
{ "+", (checkSet, part) => {
}}
};
public static Dictionary<string, Regex> leftMatch = new Dictionary<string, Regex>();
public static Regex origPOS = match["POS"];
static _selectors()
{
foreach (var type in match.Keys)
{
_selectors.match[type] = new Regex(match[type].ToString() + @"(?![^\[]*\])(?![^\(]*\))");
_selectors.leftMatch[type] = new Regex(@"(^(?:.|\r|\n)*?)" + Regex.Replace(match[type].ToString(), @"\\(\d+)", (m) =>
@"\" + (m.Index + 1)));
}
}
}
为什么我不能更改 c'tor 中的这些值?
This class is throwing an exception. It doesn't show me the exact line number, but it sounds like it's occurring in the static constructor:
static class _selectors
{
public static string[] order = new[] { "ID", "NAME", "TAG" };
public static Dictionary<string, Regex> match = new Dictionary<string, Regex> {
{ "ID", new Regex(@"#((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "CLASS", new Regex(@"\.((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "NAME", new Regex(@"\[name=['""]*((?:[\w\u00c0-\uFFFF-]|\\.)+)['""]*\]") },
{ "ATTR", new Regex(@"\[\s*((?:[\w\u00c0-\uFFFF-]|\\.)+)\s*(?:(\S?=)\s*(['""]*)(.*?)\3|)\s*\]") },
{ "TAG", new Regex(@"^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)") },
{ "CHILD", new Regex(@":(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?") },
{ "POS", new Regex(@":(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)") },
{ "PSEUDO", new Regex(@":((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\((['""]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?") }
};
public static Dictionary<string, Action<HashSet<XmlNode>, string>> relative = new Dictionary<string, Action<HashSet<XmlNode>, string>> {
{ "+", (checkSet, part) => {
}}
};
public static Dictionary<string, Regex> leftMatch = new Dictionary<string, Regex>();
public static Regex origPOS = match["POS"];
static _selectors()
{
foreach (var type in match.Keys)
{
_selectors.match[type] = new Regex(match[type].ToString() + @"(?![^\[]*\])(?![^\(]*\))");
_selectors.leftMatch[type] = new Regex(@"(^(?:.|\r|\n)*?)" + Regex.Replace(match[type].ToString(), @"\\(\d+)", (m) =>
@"\" + (m.Index + 1)));
}
}
}
Why can't I change those values in the c'tor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您查看内部异常,您将看到它指出
这意味着您正在更改正在循环的集合,这是不允许的。
而是将你的构造函数更改为类似的东西
If you view the inner exception, you will see that it states
This means you are changing the collection you are looping, which is not allowed.
Rather change your constructor to something like
您在枚举集合时修改它。你不能那样做。快速修复是将键移动到不同的集合中并枚举:
此外,如果您检查了内部异常,您会发现情况确实如此。
You're modifying a collection while enumerating it. You can't do that. Quick fix is to move the keys into a different collection and enumerate that:
Also, if you had checked the inner exception you would have seen that was the case.
简单的诊断方法:将所有代码移至正常方法中,并找出以这种方式抛出的异常。或者只是在调试器中运行它 - 当抛出异常时应该中断。
我怀疑这将是一个糟糕的正则表达式或类似的东西。
就我个人而言,我认为静态构造函数中的逻辑太多了,但这是一个稍微不同的问题...请注意,您还依赖于此处初始化的顺序:
保证目前还好,但它很脆弱。
Simple diagnostic approach: Move all that code into normal methods, and find out what exception is being thrown that way. Or just run it in the debugger - that should break when the exception is thrown.
I suspect it'll be a bad regular expression or something like that.
Personally, I think this is too much logic in a static constructor, but that's a slightly different matter... Note that you're also relying on the ordering of the initialization here:
That is guaranteed to be okay at the moment, but it's very brittle.