I'm trying to simplify a method that adds an additional entry to a range of arrays. Here is the current script:
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.
//...
}
}
Depending on the range of arrays this this method is getting f... big and difficult to manage.
To simplify the function i have created an dictionary of the possible arrays:
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;
}
Now I'm trying to use that dictionary to simplified that function:
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];")
}
}
But I'm getting a "is a 'Type' but is used like a 'Variable'" Error on line 10. Additionally I have not a clue jet how to set the global variable like in:
GlobalVariables.Array1[ArrayID] = Temparray1[ArrayID];
Maybe someone of you can help me with my problem.
---------------- Update 2016-05-11:
I have tried to improve my method but know I'm getting Invalid Cast Exceptions or Access violations. (Depending on the type of variable)
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));
}
}
I have tried to convert the current array to a dynamic variable but setting but setting the new value on the last line...
TempArrayField.SetValue(typeof(GlobalVariables), Convert.ChangeType(TempDestinationArray, ArrayType));
...fails with an invalid cast exception.
Additionally if the array is of type int[] or bool[] it already fails at line...
dynamic[] TempSourceArray = (dynamic[])TempArrayField.GetValue(typeof(GlobalVariables));
with an "Access violation".
Maybe I made clear now what's I'm trying to achieve.
Aucun commentaire:
Enregistrer un commentaire