一段反射的代码

        public static bool IsLegitimateDic(object o, DataSet dicDS, out string logContent)
        {
            bool isError = false;
            logContent = string.Empty;
            PropertyInfo[] properties = o.GetType().GetProperties();
            string filter = string.Empty;
            string propName = string.Empty;
            try
            {
                foreach (PropertyInfo item in properties)
                {
                    propName = item.Name;
                    if (item.GetValue(o, null) != null)
                    {
                        string value = item.GetValue(o, null).ToString();
                        if (string.IsNullOrEmpty(value))
                            continue;
                        if (dicEnum == null)
                            isLegitimate();
                        if (dicEnum.ContainsKey(propName))
                        {
                            string tableName = dicEnum[propName];
                            filter = string.Empty;
                            switch (tableName)
                            {
                                case "RME_CarSeries":
                                    filter = "Key = " + value + " and ParentsCode = " + o.GetType().GetProperty("IntendedBrand").GetValue(o, null);
                                    break;

                                //case "RME_CarSeries2":
                                //    dicDS.Tables[tableName].DefaultView.RowFilter = "Key = " + value + " and ParentsCode = " + o.GetType().GetProperty("IntendedBrand").GetValue(o, null);
                                //    break;

                                case "RME_City":
                                    filter = "Key = " + value + " and ParentsCode = " + o.GetType().GetProperty("Province").GetValue(o, null);
                                    break;
                                case "RME_CarModel":
                                    filter = "ModelValue = " + value + " and ClassValue = " + o.GetType().GetProperty("IntendedClass").GetValue(o, null);
                                    break;
                                case "RME_Campaign":
                                    value = value.Replace(',', ',');
                                    string[] ids = value.Split(',');
                                    filter = "Key = " + ids[0];
                                    for (int i = 1; i < ids.Count(); i++)
                                    {
                                        filter += " or Key = " + ids[i];
                                    }
                                    break;
                                default:
                                    filter = "Key = " + value;
                                    break;
                            }
                            dicDS.Tables[tableName].DefaultView.RowFilter = filter;

                            if (dicDS.Tables[tableName].DefaultView.Count == 0)
                            {
                                isError = true;
                                logContent += "[" + propName + "=" + value.ToString() + "(" + filter + ")]";
                            }
                            //清空过滤条件
                            dicDS.Tables[tableName].DefaultView.RowFilter = "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isError = true;
                logContent += "[(filter=" + filter + ")(propName=" + propName + ")]";
            }

            return isError;
        }
原文地址:https://www.cnblogs.com/futao/p/2803493.html