@@ -63,6 +63,33 @@ public virtual Dictionary<string, object> Serialize()
6363 return ret ;
6464 }
6565
66+ public static object ? ChangeType ( object ? value , Type type )
67+ {
68+ // 特殊处理List<int>类型
69+ if ( type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( List < > ) )
70+ {
71+ var elementType = type . GetGenericArguments ( ) [ 0 ] ;
72+ if ( elementType == typeof ( int ) )
73+ {
74+ // 处理 List<int>
75+ if ( value is System . Text . Json . JsonElement jsonElement )
76+ {
77+ var intList = new List < int > ( ) ;
78+ foreach ( var item in jsonElement . EnumerateArray ( ) )
79+ {
80+ intList . Add ( item . GetInt32 ( ) ) ;
81+ }
82+ return intList ;
83+ }
84+ else
85+ {
86+ throw new InvalidCastException ( $ "Expected JsonElement for List<int> property!") ;
87+ }
88+ }
89+ }
90+ return Convert . ChangeType ( value , type ) ;
91+ }
92+
6693 public virtual void Deserialize ( IReadOnlyDictionary < string , object > dict )
6794 {
6895 this . GetType ( ) . GetProperties ( )
@@ -73,7 +100,15 @@ public virtual void Deserialize(IReadOnlyDictionary<string, object> dict)
73100 if ( ! dict . ContainsKey ( PropertyToKey ( p ) ) ) return ;
74101 var value = dict [ PropertyToKey ( p ) ] ;
75102 var type = p . PropertyType ;
76- p . SetValue ( this , Convert . ChangeType ( value , type ) ) ;
103+
104+ try
105+ {
106+ p . SetValue ( this , ChangeType ( value , type ) ) ;
107+ }
108+ catch ( Exception ex )
109+ {
110+ System . Diagnostics . Debug . WriteLine ( $ "属性 { p . Name } 转换失败: { ex . Message } ") ;
111+ }
77112 } ) ;
78113 }
79114
0 commit comments