有类
ObservableCollection<DataGridColumn> a
现在将想根据a克隆一个b对象,应该怎么做?

解决方案 »

  1.   

    ObservableCollection<T>类支持序列化...直接序列化再反序列化创建新对象即可...
      

  2.   

    不对...DataGridColumn是控件,可能不支持序列化...你先说说你为什么要这样做?
      

  3.   

    DataGridColumn是不支持序列化这样做的原因目的是让一组DataGridColumn 可以属于多个DataGrid
    因为我的程序经常报错: 列已经属于 DataGrid 实例,无法重新分配。
    所以才想出了这么一个办法
      

  4.   

    DataGridColumn只是用于显示交互的...把数据和UI分开,别投机取巧...
      

  5.   

    我已经把数据和UI分开了. 现在是用代码动态的将DataGridColumn和DataGrid绑定,问题是一个DataGridColumn不能同时属于DataGrid
    所以就想出了克隆一组新的DataGridColumn的方法呀
      

  6.   

    之前也遇到过这样的问题,需要克隆实现IEnumerable接口的类,用深拷贝完成的,LZ请参考下:
      public abstract class SytelModel
        {
            #region        /// <summary>
            /// 拷贝对象
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public virtual object Clone(params object[] obj)
            {
                object newObj = null;
                Type mType = this.GetType();
                ConstructorInfo[] constructArr = mType.GetConstructors();
                if (constructArr != null && constructArr.Length > 0)
                {
                    newObj = Activator.CreateInstance(mType, this.GetConstructParams());
                    FieldInfo[] mFields = mType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (mFields != null && mFields.Length > 0)
                    {
                        foreach (FieldInfo info in mFields)
                        {
                            this.SetFieldValue(info, newObj, obj);
                        }
                    }
                }
                return newObj;
            }        /// <summary>
            /// 设置对象字段
            /// </summary>
            /// <param name="type"></param>
            /// <param name="pValue"></param>
            /// <param name="newObj"></param>
            /// <param name="objs"></param>
            private void SetFieldValue(FieldInfo field, object newObj, params object[] objs)
            {
                Type mFieldType = field.FieldType;
                object pValue = field.GetValue(this);            if (pValue != null)
                {
                    //非值类型或者string类型
                    if (mFieldType != typeof(string) && !mFieldType.IsValueType)
                    {
                        if (this.IsBaseCopyObjectType(mFieldType))
                        {
                            //使用默认的方式COPY
                            if (!this.CopyDefineElement(mFieldType, ref pValue))
                            {
                                if (objs != null && objs.Length > 1)
                                {
                                    //存在循环引用
                                    if (pValue == objs[0])
                                    {
                                        pValue = objs[1];
                                    }
                                    else
                                    {
                                        pValue = ((SytelModel)pValue).Clone(new object[] { this, newObj });
                                    }
                                }
                                else
                                {
                                    pValue = ((SytelModel)pValue).Clone(new object[] { this, newObj });
                                }
                            }
                        }
                        else
                        {
                            bool flag = false;
                            Type IEnumerableType = mFieldType.GetInterface("IEnumerable", true);
                            if (IEnumerableType != null)
                            {
                                IEnumerable IEnum = (IEnumerable)pValue;
                                Type IListType = mFieldType.GetInterface("IList", true);
                                if (IListType != null)
                                {
                                    IList list = (IList)field.GetValue(newObj);
                                    foreach (object obj in IEnum)
                                    {
                                        
                                        flag = obj.GetType().IsSubclassOf(typeof(SytelModel));
                                        if (flag)
                                        {
                                            list.Add(((SytelModel)obj).Clone(new object[] { this, newObj }));
                                        }
                                        else //此处可判断是否为值类型或string类型
                                        {
                                            list.Add(obj);
                                        }
                                    }
                                    pValue = list;
                                }
                            }
                        }
                    }
                    field.SetValue(newObj, pValue);
                }
            }        #region 私有方法        private bool IsBaseCopyObjectType(Type type)
            {
                int i = -1;
                IsBaseCopyObjectType(type.BaseType, ref i);            if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }        private void IsBaseCopyObjectType(Type type, ref int i)
            {
                if (type == typeof(Object))
                {
                    i = 0;
                }
                else if (type == typeof(SytelModel))
                {
                    i = 1;
                }            if (i == -1)
                {
                    this.IsBaseCopyObjectType(type.BaseType, ref i);
                }
            }        #endregion        #region 虚方法        /// <summary>
            /// 获取构造函数的参数
            /// </summary>
            /// <returns></returns>
            protected virtual object[] GetConstructParams()
            {
                return null;
            }        /// <summary>
            /// 自定义复制处理
            /// </summary>
            /// <param name="type"></param>
            /// <param name="obj"></param>
            /// <returns></returns>
            protected virtual bool CopyDefineElement(Type type, ref object obj)
            {
                return false;
            }        #endregion        #endregion         }
      

  7.   

    数据层与具体业务层分开
    动态模版列使用ITemplate