コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Dictionary<TKey, TValue> を Hashtable に変換する拡張メソッド

ソースコード

using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Dictionary 型の拡張メソッドを管理するクラス
/// </summary>
public static class DictionaryExtensions
{
    /// <summary>
    /// 指定された Dictionary<TKey, TValue> を Hashtable に変換します
    /// </summary>
    /// <param name="self">Dictionary<TKey, TValue> 型のインスタンス</param>
    /// <returns>Hashtable 型のインスタンス</returns>
    public static Hashtable ToHashtable<TKey, TValue>( this Dictionary<TKey, TValue> self )
    {
        var result = new Hashtable();
        foreach ( var n in self )
        {
            result[ n.Key ] = n.Value;
        }
        return result;
    }
}

関連記事