@@ -10,66 +10,54 @@ namespace APIPlugin;
1010public class IgnoreMappingAttribute : Attribute { }
1111
1212[ Obsolete ( "Unnecessary" , true ) ]
13- public static class TypeMapper < S , D > where S : class where D : class
14- {
13+ public static class TypeMapper < S , D > where S : class where D : class {
1514 private static Dictionary < string , MethodInfo > _accessors = null ;
16- private static Dictionary < string , MethodInfo > FieldAccessors
17- {
18- get
19- {
20- if ( _accessors is null )
21- {
15+ private static Dictionary < string , MethodInfo > FieldAccessors {
16+ get {
17+ if ( _accessors is null ) {
2218 _accessors = new ( ) ;
2319
24- foreach ( var field in AccessTools . GetDeclaredFields ( typeof ( S ) ) . Where ( x => ! x . GetCustomAttributes ( typeof ( IgnoreMappingAttribute ) , false ) . Any ( ) ) )
25- {
26- var accessor = new DynamicMethodDefinition ( "get_" + field . Name , typeof ( object ) , new Type [ ] { typeof ( S ) } ) ;
20+ foreach ( var _field in AccessTools . GetDeclaredFields ( typeof ( S ) ) . Where ( x => ! x . GetCustomAttributes ( typeof ( IgnoreMappingAttribute ) , false ) . Any ( ) ) ) {
21+ var accessor = new DynamicMethodDefinition ( "get_" + _field . Name , typeof ( object ) , new Type [ ] { typeof ( S ) } ) ;
2722 var il = accessor . GetILProcessor ( ) ;
2823 il . Emit ( OpCodes . Ldarg_0 ) ;
29- il . Emit ( OpCodes . Ldfld , accessor . Module . ImportReference ( field ) ) ;
30- if ( field . FieldType . IsValueType )
31- il . Emit ( OpCodes . Box , field . FieldType ) ;
24+ il . Emit ( OpCodes . Ldfld , accessor . Module . ImportReference ( _field ) ) ;
25+ if ( _field . FieldType . IsValueType )
26+ il . Emit ( OpCodes . Box , _field . FieldType ) ;
3227 il . Emit ( OpCodes . Ret ) ;
33- _accessors . Add ( field . Name , accessor . Generate ( ) ) ;
28+ _accessors . Add ( _field . Name , accessor . Generate ( ) ) ;
3429 }
3530 }
3631 return _accessors ;
3732 }
3833 }
3934
4035 private static Dictionary < string , MethodInfo > _setters = null ;
41- private static Dictionary < string , MethodInfo > FieldSetters
42- {
43- get
44- {
45- if ( _setters == null )
46- {
36+ private static Dictionary < string , MethodInfo > FieldSetters {
37+ get {
38+ if ( _setters == null ) {
4739 _setters = new ( ) ;
4840
49- foreach ( var field in AccessTools . GetDeclaredFields ( typeof ( D ) ) )
50- {
51- var setter = new DynamicMethodDefinition ( "set_" + field . Name , typeof ( void ) , new Type [ ] { typeof ( D ) , typeof ( object ) } ) ;
41+ foreach ( var _field in AccessTools . GetDeclaredFields ( typeof ( D ) ) ) {
42+ var setter = new DynamicMethodDefinition ( "set_" + _field . Name , typeof ( void ) , new Type [ ] { typeof ( D ) , typeof ( object ) } ) ;
5243 var il = setter . GetILProcessor ( ) ;
5344 il . Emit ( OpCodes . Ldarg_0 ) ;
5445 il . Emit ( OpCodes . Ldarg_1 ) ;
55- il . Emit ( OpCodes . Unbox_Any , setter . Module . ImportReference ( field . FieldType ) ) ;
56- il . Emit ( OpCodes . Stfld , setter . Module . ImportReference ( field ) ) ;
46+ il . Emit ( OpCodes . Unbox_Any , setter . Module . ImportReference ( _field . FieldType ) ) ;
47+ il . Emit ( OpCodes . Stfld , setter . Module . ImportReference ( _field ) ) ;
5748 il . Emit ( OpCodes . Ret ) ;
58- _setters . Add ( field . Name , setter . Generate ( ) ) ;
49+ _setters . Add ( _field . Name , setter . Generate ( ) ) ;
5950 }
6051 }
6152 return _setters ;
6253 }
6354 }
6455
65- public static D Convert ( S source , D destination )
66- {
67- foreach ( var field in FieldAccessors )
68- {
69- object val = field . Value . Invoke ( null , new object [ ] { source } ) ;
70- if ( val is not null && FieldSetters . ContainsKey ( field . Key ) )
71- {
72- FieldSetters [ field . Key ] . Invoke ( null , new object [ ] { destination , val } ) ;
56+ public static D Convert ( S source , D destination ) {
57+ foreach ( var _field in FieldAccessors ) {
58+ object val = _field . Value . Invoke ( null , new object [ ] { source } ) ;
59+ if ( val is not null && FieldSetters . ContainsKey ( _field . Key ) ) {
60+ FieldSetters [ _field . Key ] . Invoke ( null , new object [ ] { destination , val } ) ;
7361 }
7462 }
7563
0 commit comments