示例#1
0
        //DataRow转实体
        private static T CreateItem <T>(DataRow row)
        {
            T obj = default(T);

            if (row == null)
            {
                return(obj);
            }
            obj = Activator.CreateInstance <T>();

            foreach (DataColumn column in row.Table.Columns)
            {
                var prop = obj.GetType().GetProperty(column.ColumnName);
                try
                {
                    var value = row[column.ColumnName];
                    if (prop != null)
                    {
                        prop.SetValue(obj, value, null);
                    }
                }
                catch (Exception ex)
                {
                    TyLog.Wlog($"{column.ColumnName}转换失败:{ex}");
                }
            }
            return(obj);
        }
示例#2
0
 {  //将JSON数据转化为对应的类型
     public static T JsonToObj <T>(string jsonStr)
     {
         try
         {
             using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
             {
                 return((T) new DataContractJsonSerializer(typeof(T)).ReadObject(ms));
             }
         }
         catch (Exception ex)
         {
             TyLog.Wlog(ex);
             return(default(T));
         }
     }