有类
ObservableCollection<DataGridColumn> a
现在将想根据a克隆一个b对象,应该怎么做?
ObservableCollection<DataGridColumn> a
现在将想根据a克隆一个b对象,应该怎么做?
解决方案 »
- 这句语句有什么语法错误吗?怎么改?
- 请教:如果用应用程序安装CAB包
- c# 造成oracle 多个session
- WINFORM开发软件时如何实现类似卡巴软件一样的操作界面
- 如何可以在自定义组件中重写Font属性?
- web窗体POSTBACK后如何设置焦点到指定的TextBox
- 100分求一个常用的对数据库操作的类
- 怎样快速创建16*16个相同的按扭?像"扫雷"中的一样.
- 100分询问用javascript如何判断net2.0中我选择的TreeView节点是否为根节点
- 关于设计模式,我是设计方面的弱旅。这次要做的是基于lucene的全文搜索应用
- 帮我看看,刚写了个很简单的C#程序。
- 关于界面假死的问题!
因为我的程序经常报错: 列已经属于 DataGrid 实例,无法重新分配。
所以才想出了这么一个办法
所以就想出了克隆一组新的DataGridColumn的方法呀
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 }
动态模版列使用ITemplate