@@ -137,14 +137,14 @@ public static string GetTypeDesc(this Type t)
137
137
/// 字符串类型转换为其他数据类型
138
138
/// </summary>
139
139
/// <returns></returns>
140
- public static bool TryConvertTo ( this string ? source , Type type , [ MaybeNullWhen ( false ) ] out object ? val )
140
+ public static bool TryConvertTo ( this string ? source , Type type , out object ? val )
141
141
{
142
142
var ret = true ;
143
143
val = source ;
144
144
if ( type != typeof ( string ) )
145
145
{
146
146
ret = false ;
147
- var methodInfo = Array . Find ( typeof ( ObjectExtensions ) . GetMethods ( ) , m => m . Name == nameof ( TryConvertTo ) && m . IsGenericMethod ) ;
147
+ var methodInfo = Array . Find ( typeof ( ObjectExtensions ) . GetMethods ( ) , m => m is { Name : nameof ( TryConvertTo ) , IsGenericMethod : true } ) ;
148
148
if ( methodInfo != null )
149
149
{
150
150
methodInfo = methodInfo . MakeGenericMethod ( type ) ;
@@ -159,7 +159,7 @@ public static bool TryConvertTo(this string? source, Type type, [MaybeNullWhen(f
159
159
}
160
160
161
161
/// <summary>
162
- ///
162
+ /// Tries to convert the string representation of a value to a specified type.
163
163
/// </summary>
164
164
/// <typeparam name="TValue"></typeparam>
165
165
/// <param name="source"></param>
@@ -203,7 +203,7 @@ public static bool TryConvertTo<TValue>(this string? source, [MaybeNullWhen(fals
203
203
}
204
204
205
205
/// <summary>
206
- /// 格式化为 文件大小与单位格式 字符串
206
+ /// Formats the file size into a string with appropriate units
207
207
/// </summary>
208
208
/// <param name="fileSize"></param>
209
209
/// <returns></returns>
@@ -237,4 +237,42 @@ internal static void Clone<TModel>(this TModel source, TModel item)
237
237
}
238
238
}
239
239
}
240
+
241
+ /// <summary>
242
+ /// Creates an instance of a type and ensures all class-type properties are initialized.
243
+ /// </summary>
244
+ /// <typeparam name="TItem">The type to create an instance of.</typeparam>
245
+ /// <returns>An instance of the specified type with initialized properties.</returns>
246
+ public static TItem CreateInstanceWithCascade < TItem > ( )
247
+ {
248
+ var instance = Activator . CreateInstance < TItem > ( ) ;
249
+ instance ! . EnsureInitialized ( ) ;
250
+ return instance ;
251
+ }
252
+
253
+ /// <summary>
254
+ /// Ensures that all class-type properties of the instance are initialized.
255
+ /// </summary>
256
+ /// <param name="instance">The instance to initialize properties for.</param>
257
+ private static void EnsureInitialized ( this object instance )
258
+ {
259
+ // Reflection performance needs to be optimized here
260
+ foreach ( var propertyInfo in instance . GetType ( ) . GetProperties ( ) . Where ( p => p . PropertyType . IsClass && p . PropertyType != typeof ( string ) ) )
261
+ {
262
+ var type = propertyInfo . PropertyType ;
263
+ var value = propertyInfo . GetValue ( instance , null ) ;
264
+ if ( value is null )
265
+ {
266
+ var pv = CreateInstance ( type ) ;
267
+ propertyInfo . SetValue ( instance , pv ) ;
268
+ }
269
+ }
270
+ }
271
+
272
+ private static object ? CreateInstance ( Type type )
273
+ {
274
+ var instance = Activator . CreateInstance ( type ) ;
275
+ instance ! . EnsureInitialized ( ) ;
276
+ return instance ;
277
+ }
240
278
}
0 commit comments