c# 反射 不指定

Else , 2008/10/27 11:34 , 积累些,自己也写些 , Comments(0) , Reads(962) , Via Original

        public void ObjectConvert(object oldobj, object obj)
        {
            PropertyInfo[] oldpropInfos = oldobj.GetType().GetProperties();
            int fieldCount = oldpropInfos.Length;
            for (int i = 0; i < fieldCount; i++)
            {
                PropertyInfo propInfo = obj.GetType().GetProperty(oldpropInfos[i].Name);

                if (propInfo != null)
                {
                    if (oldpropInfos.GetValue(i) != null)
                    {
                        if (propInfo.PropertyType.IsEnum)
                        {
                            propInfo.SetValue(obj, Enum.ToObject(propInfo.PropertyType, oldpropInfos.GetValue(i)), null);
                        }
                        else
                        {
                            propInfo.SetValue(obj, oldpropInfos.GetValue(i), null);
                        }
                    }
                }
            }
        }
Add a comment

Nickname

Site URI

Email

Enable HTML Enable UBB Enable Emots Hidden Remember [Login] [Register]