ArrayList al1 = new ArrayList ();
ArrayList al2 = new ArrayList (); al1.Add ("1");
al1.Add ("2"); al2 = (ArrayList)al1.Clone (); al2[0] = "One";
al2[1] = "Two";楼主,试试吧,好使
ArrayList al2 = new ArrayList (); al1.Add ("1");
al1.Add ("2"); al2 = (ArrayList)al1.Clone (); al2[0] = "One";
al2[1] = "Two";楼主,试试吧,好使
解决方案 »
- 用sqldatareader遍历数据库中符合条件的数据遇到了一点麻烦
- 请问word里的表格的边框的宽度怎么判断?
- 如何给textBox添加快捷键?
- 高分求教:希望子报表后续页不显示主报表的字段头问题,在线等,解决揭帖!!! 顶者有分
- 使用钩子阻止u盘使用
- C#最适合的开发方向是什么?
- (1)string s="win32_logicaldisk.deviceid=\"c:\"" (2)string ss="c:\" ;str ="win32_logicaldisk.deviceid=\""+ss+"\"";怎么就不一样咧
- 数据集DataSet中的表DataTable是数据库意义的表吗?能对数据集中的表进行关系运算吗?
- 关于对象实例保存的问题
- VS2013旗舰版的新建项目类型中,安装和部署中没有安装向导类型了?
- 网页图片刷新的问题
- .net流里面有没有这种事件?
ArrayList arrM = (ArrayList)arrL.Clone();OrArrayList arrL = new ArrayList();
ArrayList arrM = new ArrayList();
arrL.CopyTo(arrM);
clone是浅拷贝,没用的
***********************************************
能说详细点吗?
ArrayList al2 = new ArrayList ();al1.Add ("1");
al1.Add ("2");foreach(object obj in al1)
{
Type t = obj.GetType();
Ojbect objNew = Activator.CreateInstance(t);
al2.add(objNew)
}
这回楼主满意了吧
iamwanghui() 朋友使用的是字符串,因此可行。 luckduck(菠萝蜜) 提到的方法我都试过了,是不行的,其实这个问题的关键在于,怎么逐个复制数组中的对象!因为不是每个对象都有clone方法,并且数组中的对象类型我现在是未知的。目前能够限制的就是可以要求这些对象都是 webcontrol 的派生类的对象。我现在在研究怎么完全复制一个 webcontrol 对象
请大家针对这个问题继续指教。
public class MyButton : Button {
public MyButton() : base() {}
public MyButton Clone() {
return (MyButton) base.MemberwiseClone();
}
}然后这样:
ArrayList arrL = new ArrayList();
ArrayList arrM = new ArrayList();MyButton btnX = new MyButton();
btnX.Text = "original";
arrL.Add(btnX);
arrL.Add(new ListBox());
ArrayList arrM = new ArrayList();
for(int i = 0; i < arrL.Count; i ++)
{
arrM.Add(((MyButton)arrL[i]).Clone());
}((Button)arrL[0]).Text = "XXX";Label1.Text = ((Button)arrM[0]).Text;这样两个ArrayList的元素就不是引用同一个对象了。
ArrayList al2 = new ArrayList (); al1.Add (new Button());
al1.Add (new ListBox()); foreach(object obj in al1)
{
Type t = obj.GetType();
Object objNew = Activator.CreateInstance(t);
al2.Add(objNew);
}刚才的代码有错误,纯手写的,这回试试
我的注释中的代码是为了解决这个问题试用的各种方法,罗列了出来,而不是我要用的,我需要的就是,能够得到一个完全的 arrL 的副本而已
不过你的方法丢失了原对象的所有属性,我正在尝试用CopyBaseAttributes方法复制这些属性
浅层拷贝不能达到你的效果,因为新获得的数组元素中的引用和原数组对应的引用是一样的。
所以修改一个同时另一个也改了。
要是想实现楼主的效果岂不是要深层copy,那么楼主数组里面的相应元素对应的对象也要拷贝一分。也就是说所有的控件都要有两份。太浪费资源了。所以楼主还是不要用这种制作副本的方法了,想一些别的方法替代达到你想要的效果。一家之言,仅供参考。
{
Type t = objT.GetType();
object objM = Activator.CreateInstance(t);
((WebControl)objM).CopyBaseAttributes((WebControl)objT);
arrM.Add(objM);
}CopyBaseAttributes仍然无效,咳
我也很无奈,我在做一个接口,用来动态的往模板列添加任意的已定制的webcontrol,可惜在datagrid添加记录的时候,接口的InstantiateIn被反复调用,最后的结果就是,只有最后一条记录成功的添加了控件,我在InstantiateIn方法中修改代码,每次都重新 new arrl 以及 arrl 中的对象,结果就可以了。因此我怀疑是因为对象被多次调用,只有最后一次修改有效如果你有这方面的经验,我可以把源代码给你请你帮我调试。
我是做的接口,不方便去定义其它人试用的类呀,咳
你使用的方法的确是建立了一个ArrayList的副本但是ArrayList中的element还是reference同一块address.
我明白你的意图,我也知道解法.
可是framework 自己的类怎么办?
大哥,知道解法就说出来嘛,
这里有150分立即奉上
其它50分送给其它热心的朋友 :)嫌分少的话我可以另开贴给分 :)
我考虑过这个问题。可以我现在只是做一个接口,如果由于这个接口要求大家把所有的工作都重做是很不人道的,毕竟webcontrl 是web 应用中的绝对主力。
{
/// <summary>
/// Clone the object, and returning a reference to a cloned object.
/// </summary>
/// <returns>Reference to the new cloned
/// object.</returns>
public object Clone()
{
//First we create an instance of this specific type.
object newObject = Activator.CreateInstance( this.GetType() ); //We get the array of fields for the new type instance.
FieldInfo[] fields = newObject.GetType().GetFields(); int i = 0; foreach( FieldInfo fi in this.GetType().GetFields() )
{
//We query if the fiels support the ICloneable interface.
Type ICloneType = fi.FieldType.
GetInterface( "ICloneable" , true ); if( ICloneType != null )
{
//Getting the ICloneable interface from the object.
ICloneable IClone = (ICloneable)fi.GetValue(this); //We use the clone method to set the new value to the field.
fields[i].SetValue( newObject , IClone.Clone() );
}
else
{
// If the field doesn't support the ICloneable
// interface then just set it.
fields[i].SetValue( newObject , fi.GetValue(this) );
} //Now we check if the object support the
//IEnumerable interface, so if it does
//we need to enumerate all its items and check if
//they support the ICloneable interface.
Type IEnumerableType = fi.FieldType.GetInterface
( "IEnumerable" , true );
if( IEnumerableType != null )
{
//Get the IEnumerable interface from the field.
IEnumerable IEnum = (IEnumerable)fi.GetValue(this); //This version support the IList and the
//IDictionary interfaces to iterate on collections.
Type IListType = fields[i].FieldType.GetInterface
( "IList" , true );
Type IDicType = fields[i].FieldType.GetInterface
( "IDictionary" , true ); int j = 0;
if( IListType != null )
{
//Getting the IList interface.
IList list = (IList)fields[i].GetValue(newObject); foreach( object obj in IEnum )
{
//Checking to see if the current item
//support the ICloneable interface.
ICloneType = obj.GetType().
GetInterface( "ICloneable" , true );
if( ICloneType != null )
{
//If it does support the ICloneable interface,
//we use it to set the clone of
//the object in the list.
ICloneable clone = (ICloneable)obj; list[j] = clone.Clone();
} //NOTE: If the item in the list is not
//support the ICloneable interface then in the
//cloned list this item will be the same
//item as in the original list
//(as long as this type is a reference type). j++;
}
}
else if( IDicType != null )
{
//Getting the dictionary interface.
IDictionary dic = (IDictionary)fields[i].
GetValue(newObject);
j = 0;
foreach( DictionaryEntry de in IEnum )
{
//Checking to see if the item
//support the ICloneable interface.
ICloneType = de.Value.GetType().
GetInterface( "ICloneable" , true ); if( ICloneType != null )
{
ICloneable clone = (ICloneable)de.Value; dic[de.Key] = clone.Clone();
}
j++;
}
}
}
i++;
}
return newObject;
}
}
先取到方法:
MethodInfo mi = typeof(Object).GetMethod("MemberwiseClone", BindingFlags.NonPublic|BindingFlags.Instance);然后将arrM = arrL;这个用下面的替换:
ArrayList arrM = new ArrayList();for(int i = 0; i < arrL.Count; i ++)
{
arrM.Add(mi.Invoke(arrL[i],null));
}
创建当前 Object 的浅表副本。浅表副本创建与原始对象具有相同类型的新实例,然后复制原始对象的非静态字段。如果字段是值类型的,则对该字段执行逐位复制。如果字段是引用类型,则复制该引用但不复制被引用的对象;这样,原始对象中的引用和复本中的引用指向同一个对象。
///
/// Clone the World object.
///
public object CloneEx()
{
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
World newWorld = (World)bFormatter.Deserialize(stream);
return newWorld;
}
//换一种方式 :定义成类
public class myClass{
public static ArrayList GetArray(){
Private ArrayList arrL = new ArrayList();
Button btnX = new Button();
btnX.Text = "original";
arrL.Add(btnX);
arrL.Add(new ListBox());
return arrL;
}
}
ArrayList arrL = myClass.GetArray();
ArrayList arrM = myClass.GetArray();
现在我试一下power_mj 和 xixigongzhu(夕夕公主) 的方法,谢谢大家,另外, acewang(平平安安过一年) ,我在上面有回复,我不能定义别的类的,因为我做的是接口
果然如你所说,还是指向同一个对象不行的:(
改成这样也不行
System.Reflection.MethodInfo mi = typeof(object).GetMethod("MemberwiseClone",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance); ArrayList arrL = new ArrayList();
ArrayList arrM = new ArrayList(); Button btnX = new Button();
btnX.Text = "original";
arrL.Add(btnX);
arrL.Add(new ListBox()); foreach(object objT in arrL)
{
object objM = new object();
objM = mi.Invoke(objT,null);
arrM.Add(objM);
}
((Button)arrL[0]).Text = "XXX"; Label1.Text = ((Button)arrM[0]).Text;
报错,提示说“xxx.aspx 不是可序列化的对象”
:(