MY il y a 5 mois
Parent
commit
af0a2586a0
1 fichiers modifiés avec 35 ajouts et 0 suppressions
  1. 35 0
      JLHHJSvr/Tools/AutoInit.cs

+ 35 - 0
JLHHJSvr/Tools/AutoInit.cs

@@ -1,9 +1,11 @@
 using JLHHJSvr.LJException;
 using System;
 using System.Data.SqlClient;
+using System.Data.SqlTypes;
 using System.Diagnostics;
 using System.Linq;
 using System.Text;
+using System.Web.UI.WebControls;
 
 namespace JLHHJSvr.Tools
 {
@@ -51,5 +53,38 @@ namespace JLHHJSvr.Tools
                 }
             }
         }
+
+        public static void AutoInitS<T>(T instance)
+        {
+            foreach (var TB in instance.GetType().GetProperties())
+            {
+                var type = TB.PropertyType;
+
+                var value = TB.GetValue(instance, null);
+                if (value == null)
+                {
+                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
+                    {
+                        type = type.GetGenericArguments()[0];
+                    }
+
+                    if (type.IsAssignableFrom(typeof(string)))
+                    {
+                        value = string.Empty;
+                    }
+                    else if (type.IsAssignableFrom(typeof(int))
+                        || type.IsAssignableFrom(typeof(decimal))
+                        || type.IsAssignableFrom(typeof(byte)))
+                    {
+                        value = Activator.CreateInstance(type);
+                    }
+
+                    if (value != null)
+                    {
+                        TB.SetValue(instance, value, null);
+                    }
+                }
+            }
+        }
     }
 }