diff --git a/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs b/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs index 977867036..daf9d868e 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs @@ -62,7 +62,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms if (m == null) break; - FieldDefinition fieldDef = m.Get("fieldAccess").Single().Annotation(); + FieldDefinition fieldDef = m.Get("fieldAccess").Single().Annotation().ResolveWithinSameModule(); if (fieldDef == null) break; AttributedNode fieldOrEventDecl = typeDeclaration.Members.FirstOrDefault(f => f.Annotation() == fieldDef); @@ -106,7 +106,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms AssignmentExpression assignment = es.Expression as AssignmentExpression; if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign) break; - FieldDefinition fieldDef = assignment.Left.Annotation(); + FieldDefinition fieldDef = assignment.Left.Annotation().ResolveWithinSameModule(); if (fieldDef == null || !fieldDef.IsStatic) break; FieldDeclaration fieldDecl = typeDeclaration.Members.OfType().FirstOrDefault(f => f.Annotation() == fieldDef); diff --git a/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs index 4dd384831..9e2dc5042 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs @@ -334,7 +334,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms continue; Match m = automaticPropertyPattern.Match(property); if (m != null) { - FieldDefinition field = m.Get("fieldReference").Single().Annotation(); + FieldDefinition field = m.Get("fieldReference").Single().Annotation().ResolveWithinSameModule(); if (field.IsCompilerGenerated()) { RemoveCompilerGeneratedAttribute(property.Getter.Attributes); RemoveCompilerGeneratedAttribute(property.Setter.Attributes); diff --git a/ICSharpCode.Decompiler/CecilExtensions.cs b/ICSharpCode.Decompiler/CecilExtensions.cs index 1ef980d1f..2280eba14 100644 --- a/ICSharpCode.Decompiler/CecilExtensions.cs +++ b/ICSharpCode.Decompiler/CecilExtensions.cs @@ -159,9 +159,25 @@ namespace ICSharpCode.Decompiler return accessorMethods; } + public static FieldDefinition ResolveWithinSameModule(this FieldReference field) + { + if (field != null && field.DeclaringType.GetElementType().Module == field.Module) + return field.Resolve(); + else + return null; + } + + public static MethodDefinition ResolveWithinSameModule(this MethodReference method) + { + if (method != null && method.DeclaringType.GetElementType().Module == method.Module) + return method.Resolve(); + else + return null; + } + public static bool IsCompilerGenerated(this ICustomAttributeProvider provider) { - if (provider.HasCustomAttributes) { + if (provider != null && provider.HasCustomAttributes) { foreach (CustomAttribute a in provider.CustomAttributes) { if (a.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") return true; diff --git a/ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs b/ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs index d7b83381c..b7e3f6d29 100644 --- a/ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs +++ b/ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs @@ -130,18 +130,12 @@ namespace ICSharpCode.Decompiler.ILAst static FieldDefinition GetFieldDefinition(FieldReference field) { - if (field != null && field.DeclaringType.IsGenericInstance) - return field.Resolve(); - else - return field as FieldDefinition; + return CecilExtensions.ResolveWithinSameModule(field); } static MethodDefinition GetMethodDefinition(MethodReference method) { - if (method != null && method.DeclaringType.IsGenericInstance) - return method.Resolve(); - else - return method as MethodDefinition; + return CecilExtensions.ResolveWithinSameModule(method); } bool MatchEnumeratorCreationNewObj(ILExpression expr, out MethodDefinition ctor)