Browse Source

Fix NullReferenceException when decompiling an automatic property within a generic class.

pull/100/head
Daniel Grunwald 15 years ago
parent
commit
ddd7b9d526
  1. 4
      ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs
  2. 2
      ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs
  3. 18
      ICSharpCode.Decompiler/CecilExtensions.cs
  4. 10
      ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs

4
ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs

@ -62,7 +62,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
if (m == null) if (m == null)
break; break;
FieldDefinition fieldDef = m.Get("fieldAccess").Single().Annotation<FieldDefinition>(); FieldDefinition fieldDef = m.Get("fieldAccess").Single().Annotation<FieldReference>().ResolveWithinSameModule();
if (fieldDef == null) if (fieldDef == null)
break; break;
AttributedNode fieldOrEventDecl = typeDeclaration.Members.FirstOrDefault(f => f.Annotation<FieldDefinition>() == fieldDef); AttributedNode fieldOrEventDecl = typeDeclaration.Members.FirstOrDefault(f => f.Annotation<FieldDefinition>() == fieldDef);
@ -106,7 +106,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
AssignmentExpression assignment = es.Expression as AssignmentExpression; AssignmentExpression assignment = es.Expression as AssignmentExpression;
if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign) if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign)
break; break;
FieldDefinition fieldDef = assignment.Left.Annotation<FieldDefinition>(); FieldDefinition fieldDef = assignment.Left.Annotation<FieldReference>().ResolveWithinSameModule();
if (fieldDef == null || !fieldDef.IsStatic) if (fieldDef == null || !fieldDef.IsStatic)
break; break;
FieldDeclaration fieldDecl = typeDeclaration.Members.OfType<FieldDeclaration>().FirstOrDefault(f => f.Annotation<FieldDefinition>() == fieldDef); FieldDeclaration fieldDecl = typeDeclaration.Members.OfType<FieldDeclaration>().FirstOrDefault(f => f.Annotation<FieldDefinition>() == fieldDef);

2
ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs

@ -334,7 +334,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
continue; continue;
Match m = automaticPropertyPattern.Match(property); Match m = automaticPropertyPattern.Match(property);
if (m != null) { if (m != null) {
FieldDefinition field = m.Get("fieldReference").Single().Annotation<FieldDefinition>(); FieldDefinition field = m.Get("fieldReference").Single().Annotation<FieldReference>().ResolveWithinSameModule();
if (field.IsCompilerGenerated()) { if (field.IsCompilerGenerated()) {
RemoveCompilerGeneratedAttribute(property.Getter.Attributes); RemoveCompilerGeneratedAttribute(property.Getter.Attributes);
RemoveCompilerGeneratedAttribute(property.Setter.Attributes); RemoveCompilerGeneratedAttribute(property.Setter.Attributes);

18
ICSharpCode.Decompiler/CecilExtensions.cs

@ -159,9 +159,25 @@ namespace ICSharpCode.Decompiler
return accessorMethods; 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) public static bool IsCompilerGenerated(this ICustomAttributeProvider provider)
{ {
if (provider.HasCustomAttributes) { if (provider != null && provider.HasCustomAttributes) {
foreach (CustomAttribute a in provider.CustomAttributes) { foreach (CustomAttribute a in provider.CustomAttributes) {
if (a.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") if (a.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute")
return true; return true;

10
ICSharpCode.Decompiler/ILAst/YieldReturnDecompiler.cs

@ -130,18 +130,12 @@ namespace ICSharpCode.Decompiler.ILAst
static FieldDefinition GetFieldDefinition(FieldReference field) static FieldDefinition GetFieldDefinition(FieldReference field)
{ {
if (field != null && field.DeclaringType.IsGenericInstance) return CecilExtensions.ResolveWithinSameModule(field);
return field.Resolve();
else
return field as FieldDefinition;
} }
static MethodDefinition GetMethodDefinition(MethodReference method) static MethodDefinition GetMethodDefinition(MethodReference method)
{ {
if (method != null && method.DeclaringType.IsGenericInstance) return CecilExtensions.ResolveWithinSameModule(method);
return method.Resolve();
else
return method as MethodDefinition;
} }
bool MatchEnumeratorCreationNewObj(ILExpression expr, out MethodDefinition ctor) bool MatchEnumeratorCreationNewObj(ILExpression expr, out MethodDefinition ctor)

Loading…
Cancel
Save