最近在写程序的时候,碰到了一个 DataTable.Clone() 的问题,描述如下:
程序中,需要传入一个DataTable,并且,希望返回该表中一些修改好的数据行,程序代码如下:
public DataTable ChangeTable(DataTable dtSoc,DataTable dtTemp)
{
dtTemp=dtSoc.Clone();
foreach(DataRow row in dtSco.Rows)
{
if(row["Name"]=="mingzi")
{
row["Adress"]="111";
dtTemp.importRow(row);//这个函数忘记叫么子了
}
}
return dtTemp;
}
本以为该函数能够正确的返回修改的值,实际上,返回的结果好像是null,然后,自己对DataTable的类型有点疑惑,呵呵,因为参数传的是DataTable,是一个引用类型,函数里面,对它的任一修改,都回返回。实际上,由于程序里面有了DataTable.Clone()这个方法,使得结果不如自己希望的。
DataTable.Clone(),对象的拷贝,在c#里面,有浅拷贝和深拷贝的区别,浅拷贝,只拷贝对象的数据,对对象的修改,都将影响到原对象。深拷贝,会从新分配一个地址,保存一个对象,对新对象的修改,不会影响到原有程序。
按照程序的结果,DataTable.Clone() 应该是深拷贝。
附上微软 该函数的方法:
internal DataTable Clone(DataSet cloneDS)
{
DataTable clone = (DataTable) Activator.CreateInstance(base.GetType());
if (clone.Columns.Count > 0)
{
clone.Reset();
}
return this.CloneTo(clone, cloneDS);
}
private DataTable CloneTo(DataTable clone, DataSet cloneDS)
{
clone.tableName = this.tableName;
clone.tableNamespace = this.tableNamespace;
clone.tablePrefix = this.tablePrefix;
clone.fNestedInDataset = this.fNestedInDataset;
clone.caseSensitive = this.caseSensitive;
clone.caseSensitiveAmbient = this.caseSensitiveAmbient;
clone.culture = this.culture;
clone.displayExpression = this.displayExpression;
clone.compareInfo = this.compareInfo;
clone.compareFlags = this.compareFlags;
clone.typeName = this.typeName;
clone.repeatableElement = this.repeatableElement;
clone.MinimumCapacity = this.MinimumCapacity;
DataColumnCollection columns = this.Columns;
for (int i = 0; i < columns.Count; i++)
{
clone.Columns.Add(columns[i].Clone());
}
if (cloneDS == null)
{
for (int m = 0; m < columns.Count; m++)
{
clone.Columns[columns[m].ColumnName].Expression = columns[m].Expression;
}
}
if (this.PrimaryKey.Length > 0)
{
int length = this.PrimaryKey.Length;
DataColumn[] columnArray = new DataColumn[length];
for (int n = 0; n < length; n++)
{
columnArray[n] = clone.Columns[this.PrimaryKey[n].Ordinal];
}
clone.PrimaryKey = columnArray;
}
for (int j = 0; j < this.Constraints.Count; j++)
{
if (!(this.Constraints[j] is ForeignKeyConstraint))
{
UniqueConstraint constraint = ((UniqueConstraint) this.Constraints[j]).Clone(clone);
Constraint constraint2 = clone.Constraints.FindConstraint(constraint);
if (constraint2 != null)
{
constraint2.ConstraintName = this.Constraints[j].ConstraintName;
}
}
}
for (int k = 0; k < this.Constraints.Count; k++)
{
if (!(this.Constraints[k] is ForeignKeyConstraint) && !clone.Constraints.Contains(this.Constraints[k].ConstraintName, true))
{
clone.Constraints.Add(((UniqueConstraint) this.Constraints[k]).Clone(clone));
}
}
if (this.extendedProperties != null)
{
foreach (object obj2 in this.extendedProperties.Keys)
{
clone.ExtendedProperties[obj2] = this.extendedProperties[obj2];
}
}
return clone;
}
{
DataTable clone = (DataTable) Activator.CreateInstance(base.GetType());
if (clone.Columns.Count > 0)
{
clone.Reset();
}
return this.CloneTo(clone, cloneDS);
}
private DataTable CloneTo(DataTable clone, DataSet cloneDS)
{
clone.tableName = this.tableName;
clone.tableNamespace = this.tableNamespace;
clone.tablePrefix = this.tablePrefix;
clone.fNestedInDataset = this.fNestedInDataset;
clone.caseSensitive = this.caseSensitive;
clone.caseSensitiveAmbient = this.caseSensitiveAmbient;
clone.culture = this.culture;
clone.displayExpression = this.displayExpression;
clone.compareInfo = this.compareInfo;
clone.compareFlags = this.compareFlags;
clone.typeName = this.typeName;
clone.repeatableElement = this.repeatableElement;
clone.MinimumCapacity = this.MinimumCapacity;
DataColumnCollection columns = this.Columns;
for (int i = 0; i < columns.Count; i++)
{
clone.Columns.Add(columns[i].Clone());
}
if (cloneDS == null)
{
for (int m = 0; m < columns.Count; m++)
{
clone.Columns[columns[m].ColumnName].Expression = columns[m].Expression;
}
}
if (this.PrimaryKey.Length > 0)
{
int length = this.PrimaryKey.Length;
DataColumn[] columnArray = new DataColumn[length];
for (int n = 0; n < length; n++)
{
columnArray[n] = clone.Columns[this.PrimaryKey[n].Ordinal];
}
clone.PrimaryKey = columnArray;
}
for (int j = 0; j < this.Constraints.Count; j++)
{
if (!(this.Constraints[j] is ForeignKeyConstraint))
{
UniqueConstraint constraint = ((UniqueConstraint) this.Constraints[j]).Clone(clone);
Constraint constraint2 = clone.Constraints.FindConstraint(constraint);
if (constraint2 != null)
{
constraint2.ConstraintName = this.Constraints[j].ConstraintName;
}
}
}
for (int k = 0; k < this.Constraints.Count; k++)
{
if (!(this.Constraints[k] is ForeignKeyConstraint) && !clone.Constraints.Contains(this.Constraints[k].ConstraintName, true))
{
clone.Constraints.Add(((UniqueConstraint) this.Constraints[k]).Clone(clone));
}
}
if (this.extendedProperties != null)
{
foreach (object obj2 in this.extendedProperties.Keys)
{
clone.ExtendedProperties[obj2] = this.extendedProperties[obj2];
}
}
return clone;
}
有事Q我:
姓名:颜昌钢
联系方式:yanchanggang@boco.com.cn
电话:13886086508
QQ:95550107
公司:亿阳集团武汉分公司
移动飞信:647360243