简化扩展/缩小数组范围的方法(鸭子打字

时间:2016-05-10 13:24:15

标签: c# arrays types duck-typing

我正在尝试简化为一系列数组添加额外条目的方法。这是当前的脚本:

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));

带有“访问冲突”。

也许我现在明确表达了我想要实现的目标。

1 个答案:

答案 0 :(得分:0)

对于每个好奇的人,我找到了解决问题的方法。

以下是我提出的建议:

首先,我将数组字典更改为列表,因为现在只需要名称:

/// <summary>
/// Class that contain the names of arrays used to save settings.
/// </summary>
private class ArrayNamesList : List<string>
{
    /// <summary>
    /// Adds an array name to the list.
    /// </summary>
    /// <param name="GlobalVariableName">Name of the array variable.</param>
    internal new void Add(string GlobalVariableName)
    {
        base.Add(GlobalVariableName);
    }
}

像这种方法一样使用:

/// <summary>
/// Method that creates a list of possible batch setting array names.
/// </summary>
/// <returns>Returns a list of possible batch setting array names.</returns>
private static ArrayNamesList CreateArrayDictionary()
{
    BatchArrayNamesList PossibleBatchArrays = new BatchArrayNamesList(); // Creates a new "Batch Array Names List".
    PossibleBatchArrays.Add(nameof(GlobalVariables.ArrayOne)); // Adds the setting "1" name to the List.
    PossibleBatchArrays.Add(nameof(GlobalVariables.ArrayTwo)); // Adds the setting "2" name to the List.
    // ...
    return PossibleBatchArrays;
}

然后我创建了两个函数来扩展数组并从数组中删除一个条目:

扩展:

/// <summary>
/// Function that increases an array of unknown type to a certain length.
/// </summary>
/// <param name="CurrentArray">The array that needs to be increased in size.</param>
/// <param name="NewArraySize">The new size of the array.</param>
private static void ArrayResize(ref Array CurrentArray, int NewArraySize)
{
    if (CurrentArray.Length < NewArraySize) // Checks if the new array size is larger than the old size.
    {
        Type elementType = CurrentArray.GetType().GetElementType(); // Gets the type of the arrays elements.
        Array newArray = Array.CreateInstance(elementType, NewArraySize); // Creats a new array with the new length.
        Array.Copy(CurrentArray, newArray, CurrentArray.Length); // Transfers the data from the old array to the new array.
        CurrentArray = newArray; // Replaces the old array with the new array.
    }
    else
    {
        Debugging.ErrorHandling.ErrorHandler("The new array size is not larger than the old array size.", 1201); // Creates an event log entry and error messagebox if application is run with gui.
    }
}

像这种方法一样使用:

/// <summary>
/// Method that adds a new empty entry to all setting arrays.
/// </summary>
private static void AddEntryToSettingsArrays()
{
    ArrayNamesList CurrentArrays = CreateArrayDictionary(); // Creates a list of the currently used setting arrays.
    GlobalVariables.FolderCount++; // Increments the folder count by one.
    foreach (string CurrentArray in CurrentArrays) // Loops through the list of arrays.
    {
        FieldInfo TempArrayField = typeof(GlobalVariables).GetField(CurrentArray); // Gets the current array with reflection.
        Array TempArray = (Array)TempArrayField.GetValue(typeof(GlobalVariables)); // Gets value of the current array.
        ArrayResize(ref TempArray, GlobalVariables.FolderCount); // Resizes the array.
        Type ArrayType = TempArray.GetType().GetElementType(); // Gets the type of the array.
        TempArrayField.SetValue(ArrayType, TempArray); // Replaces the old with the new array.
    }
}

删除条目:

/// <summary>
/// Function that removes an entry from an array of unknown type.
/// </summary>
/// <param name="CurrentArray">The array an entry needs to be deleted from.</param>
/// <param name="NewArraySize">The new size of the array.</param>
/// <param name="RemoveArrayID">The ID of the arrays entry that needs to be removed.</param>
private static void ArrayResize(ref Array CurrentArray, int NewArraySize, int RemoveArrayID)
{
    if
    (
        NewArraySize <= 0 // Checks if the new array size is not negative.
        && (CurrentArray.Length - 1) == NewArraySize // And if the new array size is the same than the old array size minus one entry.
    )
    {
        Type elementType = CurrentArray.GetType().GetElementType(); // Gets the type of the arrays elements.
        Array newArray = Array.CreateInstance(elementType, NewArraySize);  // Creats a new array with the new length.
        if (NewArraySize != 0) // if the new array is not empty.
        {
            int PositionStart = RemoveArrayID + 1; // Calculates the the start position of the entrys behind the removed ID.
            int RemainingLength = newArray.Length - RemoveArrayID; // Calculates the length of the array behind the removed ID.
            if (RemoveArrayID > 0) // If the array ID that should be remove is not the first entry.
            {
                Array.Copy(CurrentArray, 0, newArray, 0, RemoveArrayID); // Copys the entrys in front of the removed ID in the new array.
            }
            else if (RemainingLength > 0) // If the array ID that should be remove is not the last entry.
            {
                Array.Copy(CurrentArray, PositionStart, newArray, RemoveArrayID, RemainingLength); // Copys the entrys behind the removed ID in the new array.
            }
        }
        CurrentArray = newArray; // Replaces the old array with the new array.
    }
    else
    {
        Debugging.ErrorHandling.ErrorHandler("The new array size has an negative value or is not one entry smaller that the old array size.", 1202); // Creates an event log entry and error messagebox if application is run with gui.
    }
}

像这种方法一样使用:

/// <summary>
/// Method that removes a certain entry from all setting arrays.
/// </summary>
/// <param name="RemoveID">ID of the setting that should be removed from the settings.</param>
private static void RemoveEntryFromSettingsArrays(int RemoveID)
{
    ArrayNamesList CurrentArrays = CreateArrayDictionary(); // Creates a list of the currently used setting arrays.
    GlobalVariables.FolderCount--; // Decrements the folder count by one.
    foreach (string CurrentArray in CurrentArrays) // Loops through the list of arrays.
    {
        FieldInfo TempArrayField = typeof(GlobalVariables).GetField(CurrentArray);// Gets the current array with reflection.
        Array TempArray = (Array)TempArrayField.GetValue(typeof(GlobalVariables)); // Gets value of the current array.
        ArrayResize(ref TempArray, GlobalVariables.FolderCount, RemoveID); // Removes an ID from the array.
        Type ArrayType = TempArray.GetType().GetElementType(); // Gets the type of the array.
        TempArrayField.SetValue(ArrayType, TempArray); // Replaces the old with the new array.
    }
}