private void VerifyDisplayClassFieldsAreValid() { if (LambdaJobDescriptionConstruction.AllowReferenceTypes) { return; } foreach (var field in ClonedFields.Values) { var typeDefinition = field.FieldType.Resolve(); if (typeDefinition.TypeReferenceEquals(LambdaJobDescriptionConstruction.ContainingMethod.DeclaringType)) { foreach (var method in ClonedMethods) { var thisLoadingInstructions = method.Body.Instructions.Where(i => i.Operand is FieldReference fr && fr.FieldType.TypeReferenceEquals(typeDefinition)); foreach (var thisLoadingInstruction in thisLoadingInstructions) { var next = thisLoadingInstruction.Next; if (next.Operand is FieldReference fr) { UserError.DC0001(method, next, fr).Throw(); } } } } if (typeDefinition.IsDelegate()) { continue; } if (!typeDefinition.IsValueType) { foreach (var clonedMethod in ClonedMethods) { var methodInvocations = clonedMethod.Body.Instructions.Where(i => i.Operand is MethodReference mr && mr.HasThis); foreach (var methodInvocation in methodInvocations) { var pushThisInstruction = CecilHelpers.FindInstructionThatPushedArg(clonedMethod, 0, methodInvocation); if (pushThisInstruction == null) { continue; } if (pushThisInstruction.Operand is FieldReference fr && fr.FieldType.TypeReferenceEquals(typeDefinition)) { UserError.DC0002(clonedMethod, methodInvocation, (MethodReference)methodInvocation.Operand).Throw(); } } } //todo: we need a better way to detect this, and a much better error message, but let's start with this stopgap version //since it's already much better than the generic DC0004 we would otherwise have thrown below. if (field.Name.Contains("_locals")) { UserError.DC0022(LambdaJobDescriptionConstruction.ContainingMethod, LambdaJobDescriptionConstruction.WithCodeInvocationInstruction).Throw(); } UserError.DC0004(LambdaJobDescriptionConstruction.ContainingMethod, LambdaJobDescriptionConstruction.WithCodeInvocationInstruction, field).Throw(); } } }