File tree Expand file tree Collapse file tree 1 file changed +37
-1
lines changed Expand file tree Collapse file tree 1 file changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Reflection ;
3
+ using OWML . Common ;
3
4
4
5
namespace OWML . Events
5
6
{
@@ -22,5 +23,40 @@ public static FieldInfo GetAnyField(this Type type, string name)
22
23
return type . GetField ( name , Flags ) ;
23
24
}
24
25
26
+ public static T GetValue < T > ( this object obj , string name )
27
+ {
28
+ var type = obj . GetType ( ) ;
29
+ var field = type . GetAnyField ( name ) ;
30
+ if ( field != null )
31
+ {
32
+ return ( T ) field . GetValue ( obj ) ;
33
+ }
34
+ var property = type . GetAnyProperty ( name ) ;
35
+ if ( property != null )
36
+ {
37
+ return ( T ) property . GetValue ( obj , null ) ;
38
+ }
39
+ ModBehaviour . ModHelper . Console . WriteLine ( $ "Couldn't find field or property with name { name } on { type . Name } ") ;
40
+ return default ;
41
+ }
42
+
43
+ public static void SetValue ( this object obj , string name , object value )
44
+ {
45
+ var type = obj . GetType ( ) ;
46
+ var field = type . GetAnyField ( name ) ;
47
+ if ( field != null )
48
+ {
49
+ field . SetValue ( obj , value ) ;
50
+ return ;
51
+ }
52
+ var property = type . GetAnyProperty ( name ) ;
53
+ if ( property != null )
54
+ {
55
+ property . SetValue ( obj , value , null ) ;
56
+ return ;
57
+ }
58
+ ModBehaviour . ModHelper . Console . WriteLine ( $ "Couldn't find field or property with name { name } on { type . Name } ") ;
59
+ }
60
+
25
61
}
26
- }
62
+ }
You can’t perform that action at this time.
0 commit comments