反射

using System.Reflection;

 

public static string Join<T>(List<T> pListForJoin, string pPropertyName, string pSeparator, string pEnclosed)
        {
            if (pListForJoin.Count > 0)
            {
                List<Object> ltgAfterFilter = new List<Object>();
                for (int i = 0; i < pListForJoin.Count; i++)
                {
                    foreach (PropertyInfo p in pListForJoin[i].GetType().GetProperties())
                    {
                        if (p.Name.ToUpper().Equals(pPropertyName.ToUpper()))
                        {
                            if (p.GetValue(pListForJoin[i]) == null)
                            {
                            }
                            else
                            {
                                ltgAfterFilter.Add(p.GetValue(pListForJoin[i]));
                                break;
                            }
                        }
                    }
                    //没有get;set;时使用的
                    foreach (FieldInfo f in pListForJoin[i].GetType().GetFields())
                    {
                        if (f.Name.ToUpper().Equals(pPropertyName.ToUpper()))
                        {
                            if (f.GetValue(pListForJoin[i]) == null)
                            {
                            }
                            else
                            {
                                ltgAfterFilter.Add(f.GetValue(pListForJoin[i]));
                                break;
                            }
                        }
                    }
                }
                return Join(ltgAfterFilter, pSeparator, pEnclosed);
            }
            else
            {
                return "";
            }

        }

posted on 2023-08-02 16:30  *tfe*  阅读(8)  评论(0编辑  收藏  举报

导航