HashSet <TypeDefinition> AllPossibleStackTypes(MethodDefinition method) { if (!method.HasBody) { throw new ArgumentException("Method does not have body", nameof(method)); } var body = method.Body; var types = new HashSet <TypeDefinition> (); foreach (VariableDefinition var in body.Variables) { AddIfResolved(types, var.VariableType); } foreach (var parameter in body.Method.Parameters) { AddIfResolved(types, parameter.ParameterType); } foreach (ExceptionHandler eh in body.ExceptionHandlers) { if (eh.HandlerType == ExceptionHandlerType.Catch) { AddIfResolved(types, eh.CatchType); } } foreach (Instruction instruction in body.Instructions) { if (instruction.Operand is FieldReference fieldReference) { AddIfResolved(types, context.TryResolveFieldDefinition(fieldReference)?.FieldType); } else if (instruction.Operand is MethodReference methodReference) { if (methodReference is GenericInstanceMethod genericInstanceMethod) { AddFromGenericInstance(types, genericInstanceMethod); } if (methodReference.DeclaringType is GenericInstanceType genericInstanceType) { AddFromGenericInstance(types, genericInstanceType); } var resolvedMethod = context.TryResolveMethodDefinition(methodReference); if (resolvedMethod != null) { if (resolvedMethod.HasParameters) { foreach (var param in resolvedMethod.Parameters) { AddIfResolved(types, param.ParameterType); } } AddFromGenericParameterProvider(types, resolvedMethod); AddFromGenericParameterProvider(types, resolvedMethod.DeclaringType); AddIfResolved(types, resolvedMethod.ReturnType); } } } return(types); }