5

文字列「somekey = somevalue」のリストとしてフォーマットされた設定ファイルから辞書を作成したいと考えています。次に、あるクラスによって生成されたこのキーと値の辞書をプログラム内の他のクラスで使用できるようにしたいので、別のクラスで設定を使用するたびに外部ファイルを参照する必要がなくなります。

外部ファイルを読み取り、文字列のリストを辞書に変換できるクラスを作成する最初の部分を理解しましたが、ファイル読み取りクラスによって作成された辞書データを使用できるようにする方法がわかりません同じ名前空間内の他のクラス。

4

3 に答える 3

1

dictoionary publicを制限するクラスと、そのクラスの辞書を静的にするだけです。

public class MyClass
{
    // ...
    public static Dictionary<string, string> checkSumKeys { get; set; }
    // ...
}

これを次のように呼びます

// ... 
foreach (KeyValuePair<string, string> checkDict in MyClass.checkSumKeys)
    // Do stuff...

または、辞書が静的にされていない場合は、クラスをインスタンス化する必要があります

public class MyClass
{
    // ...
    public Dictionary<string, string> checkSumKeys { get; set; }
    // ...
}

これを次のように呼びます

MyClass myclass = new MyClass();
foreach (KeyValuePair<string, string> checkDict in myClass.checkSumKeys)
    // Do stuff...

これがお役に立てば幸いです。

于 2012-07-23T10:28:12.247 に答える
1

ここで何をしているのかよくわかりません。辞書をそのクラスのパブリックプロパティにするだけではいけませんか?

1つのオプションは、パブリックプロパティを使用し、アプリケーションを初期化するときにそのクラスのインスタンスを1つ作成し(これにより、クラスコンストラクターに入力すると、ディクショナリに入力されます)、同じインスタンスを関数またはクラスに渡すことができます。外部ファイルを再度読み取る必要のないコンストラクター。

public class ReadFileClass
{
    //Can be replaced with auto property
    public Dictionary<string, string> Settings
    {
        Get{return Settings}
        Set{Settings = value}
    }

    public ReadFileClass()
    {
        //In this constructor you run the code to populate the dictionary
        ReadFile();
    }

    //Method to populate dictionary
    private void ReadFile()
    {
         //Do Something
         //Settings = result of doing something
    }
}

//First class to run in your application
public class UseFile
{
    private ReadFileClass readFile;

    public UseFile()
    {
        //This instance can now be used elsewhere and passed around
        readFile = new ReadFileClass();
    }

    private void DoSomething()
    {
        //function that takes a readfileclass as a parameter can use without making a new instance internally
        otherfunction(readFileClass);
    }
}

上記を実行することにより、オブジェクトのインスタンス化を1つだけ使用して、設定ディクショナリにデータを入力し、それを渡すだけで済みます。コストのかかるパフォーマンスに影響を与える可能性のあるデータベースまたはファイルへの複数のラウンドトリップを回避するために、この方法を何度も使用しました。インスタンス化するファイル以外の別のファイルの設定を含むクラスを使用する場合は、クラスコンストラクターにそれをパラメーターとして受け取らせるだけです。

于 2012-07-23T10:28:31.790 に答える
1

少し異なるアプローチは、拡張メソッドを使用することです。私の例はかなり基本的ですが、完全に機能します

using System.Collections.Generic;

namespace SettingsDict
{
    class Program
    {
        static void Main(string[] args)
        {
            // call the extension method by adding .Settings();
            //Dictionary<string, string> settings = new Dictionary<string, string>().Settings();

            // Or by using the property in the Constants class
            var mySettings = Constants.settings;
        }
    }

    public class Constants
    {
        public static Dictionary<string, string> settings
        {
            get
            {
                return new Dictionary<string, string>().Settings();
            }
        }
    }


    public static class Extensions
    {
        public static Dictionary<string, string> Settings(this Dictionary<string, string> myDict)
        {
            // Read and split
            string[] settings = System.IO.File.ReadAllLines(@"settings.txt");

            foreach (string line in settings)
            {
                // split on =
                var split = line.Split(new[] { '=' });

                // Break if incorrect lenght
                if (split.Length != 2)
                    continue;

                // add the values to the dictionary
                myDict.Add(split[0].Trim(), split[1].Trim());
            }
            return myDict;
        }
    }
}

settings.txtの内容

setting1=1234567890
setting2=hello
setting3=world

そしてその結果

結果

もちろん、これを独自の保護機能などで拡張する必要があります。これは代替アプローチですが、拡張メソッドを使用することはそれほど悪くありません。Extensionsクラスの機能は、Constantsクラスのpropertyメソッドに直接実装することもできます。私はそれを楽しむためにそれをしました:)

于 2012-07-23T12:21:12.610 に答える