The wackiest bug I've ever seen
I have a situation I can't explain. Look at the following code and notice how the variables c
and d
are defined and their values are strange. c
equals null
but d
doesn't. Surely this is impossible(right?) given the following things I'm 100% certain of:
- a.value is a field, not a property
- the type qualifier is a sealed reference type, with base type object
- The application is just single threaded.
- I've done 'Clean Solution'.
Now the weirdest thing is that when I stop the debug run, replace the variable reference c
in the lambda expression in the bottom line by d
and build and run again, d
is null but
c` isn't!
private static object CreateConstraints(CompositeElement constraintsElement)
{
Contract.Requires(constraintsElement != null);
var constraintTypes = from e in constraintsElement where e.DefinitionName.IsAnyOf("ConstraintType", "ConstraintTypeNamePrependedWithComma") select (Qualifier)e.Value;
var declarer = (MemberDeclaration)constraintsElem开发者_StackOverflow社区ent.Parent.Value;
GenericTypeParameterCollection genericTypeParameters = declarer.Name.Suffixes.OfType<GenericTypeParameterCollection>().First();
Element a = constraintsElement["TypeName"];
a.SetValue();
var c = a.value as Qualifier;
var d = a.value as Qualifier;
genericTypeParameters.First(gp => gp.Type == c).Constraints.AddRange(constraintTypes);
return null;
}
I've also made screenshots of the disassemblies of both runs. Personally I don't know for sure whether they're incorrect.
So I want to ask if I'm missing something which could make this perfectly normal behavior, or is there something really weird going on here?
Call me daft but I don't really see a problem.
- from the description, there is no 'bug' . (Or does your programs logic go astray?)
- all we have is a variable
c
appearing twice in a Debugger window. - one instance of
c
has the right type (and I assume the right value). - the other one is null. This must be a view of the captured
c
and since the lambda is not executing it isnull
.
So what is supposed to be the problem?
You could try putting a breakpoint inside the lambda.
Looking at your debugger screenshot from before the edit, I notice that the local "c" is listed twice, once with value null, once with a value as an instance of a qualifier. This seems weird, and makes me suspect a bug in C#'s compilation of the lambda expression. As a workaround, try using an explicit delegate instead.
Also, there is an error squiggle on the double equals in the lambda expression. Hovering over that to get the error message may provide a clue as to what is going on.
Perhaps there is an exception being thrown deep in the call stack and the exception handling is being done in such a way as to prevent the assignments from being done properly.
精彩评论