Browse Source

fix #167 - Incorrect decompilation of null as extension method this parameter

pull/252/merge
Siegfried Pammer 14 years ago
parent
commit
beff26761e
  1. 9
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  2. 6
      ICSharpCode.Decompiler/Ast/Transforms/IntroduceExtensionMethods.cs

9
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -906,7 +906,13 @@ namespace ICSharpCode.Decompiler.Ast
target = ((DirectionExpression)target).Expression; target = ((DirectionExpression)target).Expression;
target.Remove(); // detach from DirectionExpression target.Remove(); // detach from DirectionExpression
} }
if (cecilMethodDef != null && cecilMethodDef.DeclaringType.IsInterface) {
if (cecilMethodDef != null) {
// convert null.ToLower() to ((string)null).ToLower()
if (target is NullReferenceExpression)
target = target.CastTo(AstBuilder.ConvertType(cecilMethod.DeclaringType));
if (cecilMethodDef.DeclaringType.IsInterface) {
TypeReference tr = byteCode.Arguments[0].InferredType; TypeReference tr = byteCode.Arguments[0].InferredType;
if (tr != null) { if (tr != null) {
TypeDefinition td = tr.Resolve(); TypeDefinition td = tr.Resolve();
@ -917,6 +923,7 @@ namespace ICSharpCode.Decompiler.Ast
} }
} }
} }
}
} else { } else {
target = new TypeReferenceExpression { Type = AstBuilder.ConvertType(cecilMethod.DeclaringType) }; target = new TypeReferenceExpression { Type = AstBuilder.ConvertType(cecilMethod.DeclaringType) };
} }

6
ICSharpCode.Decompiler/Ast/Transforms/IntroduceExtensionMethods.cs

@ -45,7 +45,11 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
if (d != null) { if (d != null) {
foreach (var ca in d.CustomAttributes) { foreach (var ca in d.CustomAttributes) {
if (ca.AttributeType.Name == "ExtensionAttribute" && ca.AttributeType.Namespace == "System.Runtime.CompilerServices") { if (ca.AttributeType.Name == "ExtensionAttribute" && ca.AttributeType.Namespace == "System.Runtime.CompilerServices") {
mre.Target = invocation.Arguments.First().Detach(); var firstArgument = invocation.Arguments.First();
if (firstArgument is NullReferenceExpression)
firstArgument = firstArgument.ReplaceWith(expr => expr.CastTo(AstBuilder.ConvertType(d.Parameters.First().ParameterType)));
else
mre.Target = firstArgument.Detach();
if (invocation.Arguments.Any()) { if (invocation.Arguments.Any()) {
// HACK: removing type arguments should be done indepently from whether a method is an extension method, // HACK: removing type arguments should be done indepently from whether a method is an extension method,
// just by testing whether the arguments can be inferred // just by testing whether the arguments can be inferred

Loading…
Cancel
Save