|
|
|
@ -22,7 +22,7 @@ using System.Linq;
@@ -22,7 +22,7 @@ using System.Linq;
|
|
|
|
|
using ICSharpCode.Decompiler.CSharp.Syntax; |
|
|
|
|
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching; |
|
|
|
|
using ICSharpCode.Decompiler.TypeSystem; |
|
|
|
|
using Mono.Cecil; |
|
|
|
|
using SRM = System.Reflection.Metadata; |
|
|
|
|
|
|
|
|
|
namespace ICSharpCode.Decompiler.CSharp.Transforms |
|
|
|
|
{ |
|
|
|
@ -187,31 +187,35 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
@@ -187,31 +187,35 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
|
|
|
|
|
var staticCtor = members.OfType<ConstructorDeclaration>().FirstOrDefault(c => (c.Modifiers & Modifiers.Static) == Modifiers.Static); |
|
|
|
|
if (staticCtor != null) { |
|
|
|
|
IMethod ctorMethod = staticCtor.GetSymbol() as IMethod; |
|
|
|
|
MethodDefinition ctorMethodDef = context.TypeSystem.GetCecil(ctorMethod) as MethodDefinition; |
|
|
|
|
if (ctorMethodDef != null && ctorMethodDef.DeclaringType.IsBeforeFieldInit) { |
|
|
|
|
while (true) { |
|
|
|
|
ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement; |
|
|
|
|
if (es == null) |
|
|
|
|
break; |
|
|
|
|
AssignmentExpression assignment = es.Expression as AssignmentExpression; |
|
|
|
|
if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign) |
|
|
|
|
break; |
|
|
|
|
IMember fieldOrProperty = (assignment.Left.GetSymbol() as IMember)?.MemberDefinition; |
|
|
|
|
if (!(fieldOrProperty is IField || fieldOrProperty is IProperty) || !fieldOrProperty.IsStatic) |
|
|
|
|
break; |
|
|
|
|
AstNode fieldOrPropertyDecl = members.FirstOrDefault(f => f.GetSymbol() == fieldOrProperty); |
|
|
|
|
if (fieldOrPropertyDecl == null) |
|
|
|
|
break; |
|
|
|
|
if (fieldOrPropertyDecl is FieldDeclaration fd) |
|
|
|
|
fd.Variables.Single().Initializer = assignment.Right.Detach(); |
|
|
|
|
else if (fieldOrPropertyDecl is PropertyDeclaration pd) |
|
|
|
|
pd.Initializer = assignment.Right.Detach(); |
|
|
|
|
else |
|
|
|
|
break; |
|
|
|
|
es.Remove(); |
|
|
|
|
if (!ctorMethod.MetadataToken.IsNil) { |
|
|
|
|
var metadata = context.TypeSystem.ModuleDefinition.GetMetadataReader(); |
|
|
|
|
SRM.MethodDefinition ctorMethodDef = metadata.GetMethodDefinition((SRM.MethodDefinitionHandle)ctorMethod.MetadataToken); |
|
|
|
|
SRM.TypeDefinition declaringType = metadata.GetTypeDefinition(ctorMethodDef.GetDeclaringType()); |
|
|
|
|
if (declaringType.HasFlag(System.Reflection.TypeAttributes.BeforeFieldInit)) { |
|
|
|
|
while (true) { |
|
|
|
|
ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement; |
|
|
|
|
if (es == null) |
|
|
|
|
break; |
|
|
|
|
AssignmentExpression assignment = es.Expression as AssignmentExpression; |
|
|
|
|
if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign) |
|
|
|
|
break; |
|
|
|
|
IMember fieldOrProperty = (assignment.Left.GetSymbol() as IMember)?.MemberDefinition; |
|
|
|
|
if (!(fieldOrProperty is IField || fieldOrProperty is IProperty) || !fieldOrProperty.IsStatic) |
|
|
|
|
break; |
|
|
|
|
AstNode fieldOrPropertyDecl = members.FirstOrDefault(f => f.GetSymbol() == fieldOrProperty); |
|
|
|
|
if (fieldOrPropertyDecl == null) |
|
|
|
|
break; |
|
|
|
|
if (fieldOrPropertyDecl is FieldDeclaration fd) |
|
|
|
|
fd.Variables.Single().Initializer = assignment.Right.Detach(); |
|
|
|
|
else if (fieldOrPropertyDecl is PropertyDeclaration pd) |
|
|
|
|
pd.Initializer = assignment.Right.Detach(); |
|
|
|
|
else |
|
|
|
|
break; |
|
|
|
|
es.Remove(); |
|
|
|
|
} |
|
|
|
|
if (staticCtor.Body.Statements.Count == 0) |
|
|
|
|
staticCtor.Remove(); |
|
|
|
|
} |
|
|
|
|
if (staticCtor.Body.Statements.Count == 0) |
|
|
|
|
staticCtor.Remove(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|