社区
C#
帖子详情
ObservableCollection类的实例怎样克隆吗?
siaosa
2011-02-09 03:17:37
有类
ObservableCollection<DataGridColumn> a
现在将想根据a克隆一个b对象,应该怎么做?
...全文
508
8
打赏
收藏
ObservableCollection类的实例怎样克隆吗?
有类 ObservableCollection a 现在将想根据a克隆一个b对象,应该怎么做?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
wuyq11
2011-02-09
打赏
举报
回复
数据层与具体业务层分开
动态模版列使用ITemplate
abbycolor
2011-02-09
打赏
举报
回复
之前也遇到过这样的问题,需要克隆实现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
}
siaosa
2011-02-09
打赏
举报
回复
我已经把数据和UI分开了. 现在是用代码动态的将DataGridColumn和DataGrid绑定,问题是一个DataGridColumn不能同时属于DataGrid
所以就想出了克隆一组新的DataGridColumn的方法呀
vrhero
2011-02-09
打赏
举报
回复
DataGridColumn只是用于显示交互的...把数据和UI分开,别投机取巧...
siaosa
2011-02-09
打赏
举报
回复
DataGridColumn是不支持序列化
这样做的原因目的是让一组DataGridColumn 可以属于多个DataGrid
因为我的程序经常报错: 列已经属于 DataGrid 实例,无法重新分配。
所以才想出了这么一个办法
flyerwing
2011-02-09
打赏
举报
回复
[Quote=引用 1 楼 vrhero 的回复:]
ObservableCollection<T>类支持序列化...直接序列化再反序列化创建新对象即可...
[/Quote]
这个办法挺好.
vrhero
2011-02-09
打赏
举报
回复
不对...DataGridColumn是控件,可能不支持序列化...
你先说说你为什么要这样做?
vrhero
2011-02-09
打赏
举报
回复
ObservableCollection<T>类支持序列化...直接序列化再反序列化创建新对象即可...
克隆
类
(
克隆
出一个一摸一样的对象)
//
克隆
类
(
克隆
出一个一摸一样的对象) using System.IO; using System.Xml.Serialization; using System; static class CloneClass { public static Object Clone(Object needCloneObject) { //先把
类
...
C#一个对象只传递值不传递引用到另一个对象---Clone的用法
想只复制值需要你的
类
实现ICloneable接口,并实现public object Clone()方法,其中 { return this as object; //引用同一个对象 return this.MemberwiseClone(); //浅复制 return new DrawBase() as object;//深复制...
AI应用开发实战(转)
起来,成为有公开数据接口的
类
。 将输入的图片进行 规范化 ,成为数据接口能够使用的格式。 最后通过模型来推理(inference)出图片应该是哪个数字,并显示出来。 是不是很简单? 二、动手 步骤一:获取...
Entity Framework Core——6.更改跟踪(change tracker)工作原理
文章目录1. 什么时候跟踪、什么时候不跟踪?2. 实体的状态3. 手动跟踪实体3.1 附加现有实体3.2 更新现有实体3.3 删除实体3.3.1 删除子实体3.3.2 删除父实体3.4 自定义跟踪4...每个数据库上线文
实例
都会跟踪对实体的更.
WP7有约(三):课堂重点
那么,怎样才能让ListPicker"呼之则来挥之则去"? 我们可以调整ListBox的大小,使之充满整个Pivot项,并把ListPicker藏在屏幕下方外面,然后在Application Bar上放置一个按钮,当用户单击这个按钮时,...
C#
110,539
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章