@@ -11,7 +11,7 @@ namespace CodingSeb.Localization.FodyAddin.Fody
1111{
1212 public class ModuleWeaver : BaseModuleWeaver
1313 {
14- #region Attibutes
14+ #region atrributes and references
1515
1616 private const MethodAttributes staticConstructorAttributes =
1717 MethodAttributes . Private |
@@ -20,10 +20,19 @@ public class ModuleWeaver : BaseModuleWeaver
2020 MethodAttributes . SpecialName |
2121 MethodAttributes . RTSpecialName ;
2222
23+ private TypeReference weakEventManagerType ;
24+ private TypeReference locType ;
25+ private TypeReference currentLanguageChangedEventArgsType ;
26+ private TypeReference genericEventManagerType ;
27+ private MethodReference addHandlerMethod ;
28+ private MethodReference genericAddHandlerMethod ;
29+
2330 #endregion
2431
2532 public override void Execute ( )
2633 {
34+ InitReferences ( ) ;
35+
2736 ModuleDefinition
2837 . Types
2938 . Where ( typeDefinition =>
@@ -34,6 +43,16 @@ public override void Execute()
3443 . ForEach ( AddCurrentLanguageChangedSensitivity ) ;
3544 }
3645
46+ private void InitReferences ( )
47+ {
48+ weakEventManagerType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "System.Windows.WeakEventManager`2" ) ) ;
49+ locType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "CodingSeb.Localization.Loc" ) ) ;
50+ currentLanguageChangedEventArgsType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "CodingSeb.Localization.CurrentLanguageChangedEventArgs" ) ) ;
51+ genericEventManagerType = ModuleDefinition . ImportReference ( weakEventManagerType . MakeGenericInstanceType ( locType , currentLanguageChangedEventArgsType ) ) ;
52+ addHandlerMethod = ModuleDefinition . ImportReference ( genericEventManagerType . Resolve ( ) . Methods . FirstOrDefault ( m => m . Name . Equals ( "AddHandler" ) ) ) ;
53+ genericAddHandlerMethod = ModuleDefinition . ImportReference ( addHandlerMethod . MakeHostInstanceGeneric ( locType , currentLanguageChangedEventArgsType ) ) ;
54+ }
55+
3756 private void AddCurrentLanguageChangedSensitivity ( TypeDefinition typeDefinition )
3857 {
3958 IEnumerable < PropertyDefinition > propertyToLocalize = typeDefinition . Properties . Where ( property => property . HasLocalizeAttribute ( ) ) ;
@@ -46,41 +65,32 @@ private void AddCurrentLanguageChangedSensitivity(TypeDefinition typeDefinition)
4665
4766 List < MethodDefinition > exclusiveAlwaysCalledConstructors = typeDefinition . GetConstructors ( ) . Where ( constructor =>
4867 {
49- return constructor . Body . Instructions . Count > 2 &&
68+ return ! constructor . IsStatic &&
69+ constructor . Body . Instructions . Count > 2 &&
5070 constructor . Body . Instructions [ 1 ] . OpCode == OpCodes . Call &&
5171 constructor . Body . Instructions [ 1 ] . Operand is MethodReference methodReference &&
5272 methodReference . DeclaringType != typeDefinition ;
5373 } ) . ToList ( ) ;
5474
55- var weakEventManagerType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "System.Windows.WeakEventManager`2" ) ) ;
56- var locType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "CodingSeb.Localization.Loc" ) ) ;
57- var currentLanguageChangedEventArgsType = ModuleDefinition . ImportReference ( FindTypeDefinition ( "CodingSeb.Localization.CurrentLanguageChangedEventArgs" ) ) ;
58- var genericEventManagerType = ModuleDefinition . ImportReference ( weakEventManagerType . MakeGenericInstanceType ( locType , currentLanguageChangedEventArgsType ) ) ;
59- //var resolvedEventManagerType = ModuleDefinition.ImportReference(genericEventManagerType.Resolve());
60- var addHandlerMethod = ModuleDefinition . ImportReference ( genericEventManagerType . Resolve ( ) . Methods . FirstOrDefault ( m => m . Name . Equals ( "AddHandler" ) ) ) ;
61- var genericAddHandlerMethod = ModuleDefinition . ImportReference ( addHandlerMethod . MakeHostInstanceGeneric ( locType , currentLanguageChangedEventArgsType ) ) ;
62-
63- var tempMethod = typeDefinition . Methods . FirstOrDefault ( m => m . Name . Equals ( "MyMethod" ) ) ;
64-
65- //exclusiveAlwaysCalledConstructors.ForEach(constructor =>
66- //{
67- // var instructions = constructor.Body.Instructions;
68-
69- // if (instructions.LastOrDefault(i => i.OpCode == OpCodes.Ret) is Instruction instruction)
70- // {
71- // instructions.Remove(instruction);
72- // }
73-
74- // instructions.Add(Instruction.Create(OpCodes.Nop));
75- // instructions.Add(Instruction.Create(OpCodes.Call, ModuleDefinition.ImportReference(typeof(Loc).GetProperty("Instance").GetGetMethod())));
76- // instructions.Add(Instruction.Create(OpCodes.Ldstr, "CurrentLanguageChanged"));
77- // instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
78- // instructions.Add(Instruction.Create(OpCodes.Ldftn, currentLanguageChangedMethod));
79- // instructions.Add(Instruction.Create(OpCodes.Newobj, ModuleDefinition.ImportReference(typeof(CurrentLanguageChangedEventArgs).GetConstructors()[0])));
80- // instructions.Add(Instruction.Create(OpCodes.Call, genericAddHandlerMethod));
81- // instructions.Add(Instruction.Create(OpCodes.Nop));
82- // instructions.Add(Instruction.Create(OpCodes.Ret));
83- //});
75+ exclusiveAlwaysCalledConstructors . ForEach ( constructor =>
76+ {
77+ var instructions = constructor . Body . Instructions ;
78+
79+ if ( instructions . LastOrDefault ( i => i . OpCode == OpCodes . Ret ) is Instruction instruction )
80+ {
81+ instructions . Remove ( instruction ) ;
82+ }
83+
84+ instructions . Add ( Instruction . Create ( OpCodes . Nop ) ) ;
85+ instructions . Add ( Instruction . Create ( OpCodes . Call , ModuleDefinition . ImportReference ( typeof ( Loc ) . GetProperty ( "Instance" ) . GetGetMethod ( ) ) ) ) ;
86+ instructions . Add ( Instruction . Create ( OpCodes . Ldstr , "CurrentLanguageChanged" ) ) ;
87+ instructions . Add ( Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
88+ instructions . Add ( Instruction . Create ( OpCodes . Ldftn , currentLanguageChangedMethod ) ) ;
89+ instructions . Add ( Instruction . Create ( OpCodes . Newobj , ModuleDefinition . ImportReference ( typeof ( EventHandler < CurrentLanguageChangedEventArgs > ) . GetConstructors ( ) [ 0 ] ) ) ) ;
90+ instructions . Add ( Instruction . Create ( OpCodes . Call , genericAddHandlerMethod ) ) ;
91+ instructions . Add ( Instruction . Create ( OpCodes . Nop ) ) ;
92+ instructions . Add ( Instruction . Create ( OpCodes . Ret ) ) ;
93+ } ) ;
8494 }
8595
8696 private MethodDefinition AddCurrentLanguageChangedMethod ( TypeDefinition typeDefinition , FieldDefinition propertyListFieldDefinition , MethodDefinition triggerPropertyChangedMethod )
0 commit comments