[英]C# Array Move Item (Not ArrayList/Generic List)
Have an object which is an array (not arraylist or generic) that could hold a set of anything...有一个 object 是一个数组(不是 arraylist 或通用),可以容纳一组任何东西......
[[One],[Two],[Three],[Four]]
Want to move [Four] to in front of [Two] eg oldIndex = 3, newIndex = 1 so the result would be...想要将 [Four] 移动到 [Two] 的前面,例如 oldIndex = 3, newIndex = 1 所以结果将是......
[[One],[Four][Two],[Three]]
Whats the most effient way to do this in .NET 2.0, eg在 .NET 2.0 中执行此操作的最有效方法是什么,例如
PropertyInfo oPI = ObjectType.GetProperty("MyArray", BindingFlags.Public | BindingFlags.Instance);
object ObjectToReorder = oPI.GetValue(ParentObject, null);
Array arr = ObjectToReorder as Array;
int oldIndex = 3;
int newIndex = 1;
//Need the re-ordered list still attached to the ParentObject
thanks in advance提前致谢
void MoveWithinArray(Array array, int source, int dest)
{
Object temp = array.GetValue(source);
Array.Copy(array, dest, array, dest + 1, source - dest);
array.SetValue(temp, dest);
}
Try this尝试这个
Array someArray = GetTheArray();
object temp = someArray.GetValue(3);
someArray.SetValue(someArray.GetValue(1), 3);
someArray.SetValue(temp, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.