@@ -137,14 +137,14 @@ public static string GetTypeDesc(this Type t)
137137 /// 字符串类型转换为其他数据类型
138138 /// </summary>
139139 /// <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 )
141141 {
142142 var ret = true ;
143143 val = source ;
144144 if ( type != typeof ( string ) )
145145 {
146146 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 } ) ;
148148 if ( methodInfo != null )
149149 {
150150 methodInfo = methodInfo . MakeGenericMethod ( type ) ;
@@ -159,7 +159,7 @@ public static bool TryConvertTo(this string? source, Type type, [MaybeNullWhen(f
159159 }
160160
161161 /// <summary>
162- ///
162+ /// Tries to convert the string representation of a value to a specified type.
163163 /// </summary>
164164 /// <typeparam name="TValue"></typeparam>
165165 /// <param name="source"></param>
@@ -203,7 +203,7 @@ public static bool TryConvertTo<TValue>(this string? source, [MaybeNullWhen(fals
203203 }
204204
205205 /// <summary>
206- /// 格式化为 文件大小与单位格式 字符串
206+ /// Formats the file size into a string with appropriate units
207207 /// </summary>
208208 /// <param name="fileSize"></param>
209209 /// <returns></returns>
@@ -237,4 +237,42 @@ internal static void Clone<TModel>(this TModel source, TModel item)
237237 }
238238 }
239239 }
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+ }
240278}
0 commit comments