【发布时间】:2016-05-10 13:24:15
【问题描述】:
我正在尝试简化一种将附加条目添加到数组范围的方法。这是当前脚本:
private static void AddEntryToSettingsArrays()
{
string[] Temparray1 = GlobalVariables.Array1; // Copys the setting array "1" to a temporary array.
int[] Temparray2 = GlobalVariables.Array2; // Copys the setting array "2" to a temporary array.
//...
GlobalVariables.ArrayCount++; // Increments the Array count by one.
GlobalVariables.Array1 = new string[GlobalVariables.ArrayCount]; // Clears array "1" and creates a new array with the new array count.
GlobalVariables.Array2 = new int[GlobalVariables.ArrayCount]; // Clears array "2" and creates a new array with the new array count.
//...
for (int ArrayID = 0; ArrayID < GlobalVariables.ArrayCount - 1; ArrayID++) //Loops through the arrays until the next to last array is reached.
{
GlobalVariables.Array1[ArrayID] = Temparray1[ArrayID]; // Copys the "1" temporary array back to the global array.
GlobalVariables.Array2[ArrayID] = Temparray2[ArrayID]; // Copys the "2" temporary array back to the global array.
//...
}
}
根据数组的范围,此方法变得...大且难以管理。
为了简化函数,我创建了一个可能数组的字典:
private static BatchArraysDictionary CreateBatchArrayDictionary()
{
BatchArraysDictionary PossibleBatchArrays = new BatchArraysDictionary(); // Creates a new "Batch Array Dictionary".
PossibleBatchArrays.Add(nameof(GlobalVariables.Array1), GlobalVariables.Array1.GetType());
PossibleBatchArrays.Add(nameof(GlobalVariables.Array2), GlobalVariables.Array2.GetType());
//...
return PossibleBatchArrays;
}
现在我正在尝试使用该字典来简化该功能:
private static void AddEntryToSettingsArrays()
{
BatchArraysDictionary CurrentArrays = CreateBatchArrayDictionary();
GlobalVariables.ArrayCount++; // Increments the folder count by one.
foreach (KeyValuePair<string, Type> CurrentArray in CurrentArrays)
{
Type CurrentType = CurrentArray.Value;
var TempArray = typeof(GlobalVariables).GetField(CurrentArray.Key).GetValue(CurrentType);
var GlobalArray = new CurrentType[GlobalVariables.ArrayCount]; // Getting the is a 'Type' but is used like a 'Variable' Error here...
for(int ArrayID = 0; ArrayID < GlobalVariables.ArrayCount - 1; ArrayID++) //Loops through the Folders until the next to last folder is reached.
{
// Not sure what to do here jet (What's the equivalent to "GlobalVariables.Array1[ArrayID] = Temparray1[ArrayID];")
}
}
但我在第 10 行收到“是一个‘类型’但用作‘变量’”的错误。 此外,我不知道如何设置全局变量,如:
GlobalVariables.Array1[ArrayID] = Temparray1[ArrayID];
也许你们中的某个人可以帮助我解决我的问题。
---------------- 2016-05-11 更新:
我已尝试改进我的方法,但我知道我遇到了无效的强制转换异常或访问冲突。 (取决于变量的类型)
private static void AddEntryToSettingsArrays()
{
BatchArraysDictionary CurrentArrays = CreateBatchArrayDictionary();
GlobalVariables.FolderCount++; // Increments the folder count by one.
foreach (KeyValuePair<string, Type> CurrentArray in CurrentArrays)
{
Type ArrayType = CurrentArray.Value;
//Console.WriteLine("Array Type (Stored): " + ArrayType);
FieldInfo TempArrayField = typeof(GlobalVariables).GetField(CurrentArray.Key);
//Console.WriteLine("Array Type (Field): " + TempArrayField.GetValue(typeof(GlobalVariables)));
dynamic[] TempSourceArray = (dynamic[])TempArrayField.GetValue(typeof(GlobalVariables));
//Console.WriteLine("Array loaded.");
dynamic[] TempDestinationArray = new dynamic[GlobalVariables.FolderCount];
//Console.WriteLine("New Array created.");
for (int ArrayID = 0; ArrayID < GlobalVariables.FolderCount - 1; ArrayID++) //Loops through the Folders until the next to last folder is reached.
{
TempDestinationArray[ArrayID] = TempSourceArray[ArrayID];
//Console.WriteLine("Array ID " + ArrayID + " was copied to new array");
}
//Console.WriteLine("Copyprccess finished.");
//Console.WriteLine("Testentry:" + TempDestinationArray[0]);
TempArrayField.SetValue(typeof(GlobalVariables), Convert.ChangeType(TempDestinationArray, ArrayType));
}
}
我尝试将当前数组转换为动态变量,但在最后一行设置但设置新值...
TempArrayField.SetValue(typeof(GlobalVariables), Convert.ChangeType(TempDestinationArray, ArrayType));
...因无效转换异常而失败。
此外,如果数组是 int[] 或 bool[] 类型,它已经在行...
dynamic[] TempSourceArray = (dynamic[])TempArrayField.GetValue(typeof(GlobalVariables));
带有“访问冲突”。
也许我现在已经明确了我想要实现的目标。
【问题讨论】:
-
第10行是哪一个?
-
var GlobalArray = new CurrentType[GlobalVariables.ArrayCount]; // 获取的是一个“类型”,但在这里像一个“变量”错误一样使用......
-
我明白了,您的
CurrentType是一个变量,正如您之前在两行中看到的那样。这就是为什么你有这个错误。如果您想根据CurrentType创建一个变量,则您正在进行后期绑定。你必须使用Reflection。 -
如果 CurrentArray.Value 是 System.Int32[] 数组。如何使用 size 参数创建新数组...
-
您的意思是创建以大小为参数的数组?
new int[size]
标签: c# arrays types duck-typing