@@ -39,10 +39,29 @@ private void AddCurrentLanguageChangedSensitivity(TypeDefinition typeDefinition)
39
39
40
40
FieldDefinition propertyListFieldDefinition = AddLocalizedPropertyNamesStaticList ( typeDefinition , propertyToLocalize . Select ( p => p . Name ) ) ;
41
41
42
+ MethodDefinition triggerPropertyChangedMethod = typeDefinition . FindPropertyChangedTriggerMethod ( ) ;
43
+
44
+ if ( triggerPropertyChangedMethod == null )
45
+ {
46
+ triggerPropertyChangedMethod = new MethodDefinition ( "__NotifyPropertyChanged__" , MethodAttributes . Private , TypeSystem . VoidReference ) ;
47
+ triggerPropertyChangedMethod . Parameters . Add ( new ParameterDefinition ( "propertyName" , ParameterAttributes . None , ModuleDefinition . ImportReference ( typeof ( string ) ) ) ) ;
48
+ typeDefinition . Methods . Add ( triggerPropertyChangedMethod ) ;
49
+
50
+ // TODO
51
+ }
52
+
42
53
var method = new MethodDefinition ( "__CurrentLanguageChanged__" , MethodAttributes . Private , TypeSystem . VoidReference ) ;
54
+ method . Parameters . Add ( new ParameterDefinition ( "sender" , ParameterAttributes . None , TypeSystem . ObjectReference ) ) ;
55
+ method . Parameters . Add ( new ParameterDefinition ( "e" , ParameterAttributes . None , ModuleDefinition . ImportReference ( typeof ( CurrentLanguageChangedEventArgs ) ) ) ) ;
43
56
ILProcessor processor = method . Body . GetILProcessor ( ) ;
44
- processor . Append ( Instruction . Create ( OpCodes . Ldstr , "Hello World" ) ) ;
45
- processor . Append ( Instruction . Create ( OpCodes . Ret ) ) ;
57
+ processor . Emit ( OpCodes . Nop ) ;
58
+ processor . Emit ( OpCodes . Ldsfld , propertyListFieldDefinition ) ;
59
+ processor . Emit ( OpCodes . Ldarg_0 ) ;
60
+ processor . Emit ( OpCodes . Ldftn , triggerPropertyChangedMethod ) ;
61
+ processor . Emit ( OpCodes . Newobj , ModuleDefinition . ImportReference ( typeof ( Action < string > ) . GetConstructors ( ) . First ( ) ) ) ;
62
+ processor . Emit ( OpCodes . Callvirt , ModuleDefinition . ImportReference ( typeof ( List < string > ) . GetMethod ( "ForEach" , new Type [ ] { typeof ( Action < string > ) } ) ) ) ;
63
+ processor . Emit ( OpCodes . Nop ) ;
64
+ processor . Emit ( OpCodes . Ret ) ;
46
65
typeDefinition . Methods . Add ( method ) ;
47
66
}
48
67
@@ -56,37 +75,37 @@ public FieldDefinition AddLocalizedPropertyNamesStaticList(TypeDefinition typeDe
56
75
57
76
typeDefinition . Fields . Add ( field ) ;
58
77
59
- MethodDefinition staticConstructor = GetOrCreateStaticConstructor ( typeDefinition ) ;
78
+ MethodDefinition staticConstructor = typeDefinition . GetStaticConstructor ( ) ;
79
+
80
+ if ( staticConstructor == null )
81
+ {
82
+ staticConstructor = new MethodDefinition ( ".cctor" , staticConstructorAttributes , TypeSystem . VoidReference ) ;
83
+ typeDefinition . Methods . Add ( staticConstructor ) ;
84
+ }
60
85
61
- var il = staticConstructor . Body . GetILProcessor ( ) ;
86
+ var instructions = staticConstructor . Body . Instructions ;
62
87
63
- if ( il . Body . Instructions . LastOrDefault ( i => i . OpCode == OpCodes . Ret ) is Instruction instruction )
88
+ if ( instructions . LastOrDefault ( i => i . OpCode == OpCodes . Ret ) is Instruction instruction )
64
89
{
65
- il . Remove ( instruction ) ;
90
+ instructions . Remove ( instruction ) ;
66
91
}
67
92
68
- il . Emit ( OpCodes . Newobj , constructorOnStringList ) ;
93
+ instructions . Add ( Instruction . Create ( OpCodes . Newobj , constructorOnStringList ) ) ;
69
94
70
- // propertiesNames.ToList().ForEach(propertyName =>
71
- // {
72
- // instructions.Add(Instruction.Create(OpCodes.Dup));
73
- // instructions.Add(Instruction.Create(OpCodes.Ldstr, propertyName));
74
- // instructions.Add(Instruction.Create(OpCodes.Callvirt, addMethodOnStringList));
75
- // });
95
+ propertiesNames . ToList ( ) . ForEach ( propertyName =>
96
+ {
97
+ instructions . Add ( Instruction . Create ( OpCodes . Dup ) ) ;
98
+ instructions . Add ( Instruction . Create ( OpCodes . Ldstr , propertyName ) ) ;
99
+ instructions . Add ( Instruction . Create ( OpCodes . Callvirt , addMethodOnStringList ) ) ;
100
+ } ) ;
76
101
77
- il . Emit ( OpCodes . Stsfld , field ) ;
102
+ instructions . Add ( Instruction . Create ( OpCodes . Stsfld , field ) ) ;
78
103
79
- il . Emit ( OpCodes . Ret ) ;
104
+ instructions . Add ( Instruction . Create ( OpCodes . Ret ) ) ;
80
105
81
106
return field ;
82
107
}
83
108
84
- public MethodDefinition GetOrCreateStaticConstructor ( TypeDefinition typeDefinition )
85
- {
86
- return typeDefinition . GetStaticConstructor ( ) ??
87
- new MethodDefinition ( ".cctor" , staticConstructorAttributes , TypeSystem . VoidReference ) ;
88
- }
89
-
90
109
public override IEnumerable < string > GetAssembliesForScanning ( )
91
110
{
92
111
yield return "netstandard" ;
0 commit comments