2

私は以下のテキストを持っています:

id=1
familyName=Rooney
givenName=Wayne
middleNames=Mark
dateOfBirth=1985-10-24
dateOfDeath=
placeOfBirth=Liverpool
height=1.76
twitterId=@WayneRooney

行は「\n」で区切られ、ペアは「=」で区切られます。

Id、FamilyName、GivenName などのプロパティを持つ Person クラスがあります。

上記のテキストを Person オブジェクトにデシリアライズし、後で Person オブジェクトを正しい行区切りとペア区切りで上記のテキストにシリアライズする簡単な方法はありますか?

TextSerializer のようなものがあるといいのですが?

基本的に、person1.txt などのファイルからテキストを読み取り、それを Person オブジェクトに逆シリアル化する必要があります。

可能であれば、プロパティごとに手動でハードコーディングすることは避けたいと思います。ありがとう、

4

3 に答える 3

2

リフレクションは、プロパティ名をハードコーディングしたり、サードパーティのライブラリを使用したりすることなく、ここで役立ちます

var person = Deserialize<Person2>("a.txt");

T Deserialize<T>(string fileName)
{
    Type type = typeof(T);
    var obj = Activator.CreateInstance(type);
    foreach (var line in File.ReadLines(fileName))
    {
        var keyVal = line.Split('=');
        if (keyVal.Length != 2) continue;

        var prop = type.GetProperty(keyVal[0].Trim());
        if (prop != null)
        {
            prop.SetValue(obj, Convert.ChangeType(keyVal[1], prop.PropertyType));
        }
    }
    return (T)obj;
}

public class Person2
{
    public int id { set; get; }
    public string familyName { set; get; }
    public string givenName { set; get; }
    public string middleNames { set; get; }
    public string dateOfBirth { set; get; }
    public string dateOfDeath { set; get; }
    public string placeOfBirth { set; get; }
    public double height { set; get; }
    public string twitterId { set; get; }
}
于 2012-12-04T18:20:38.020 に答える
1

これも可能な解決策です。可能であれば、作成時にテキストを json としてフォーマットすることを試みることができます。したがって、このすべての治療は必要ありません。Json.netを使用するだけです

public class Person
{
    public int id { set; get; }
    public string familyName { set; get; }
    public string givenName { set; get; }
    public string middleNames { set; get; }
    public string dateOfBirth { set; get; }
    public string dateOfDeath { set; get; }
    public string placeOfBirth { set; get; }
    public double height { set; get; }
    public string twitterId { set; get; }
}

class Program
{
    static void Main(string[] args)
    {
        string line;
        string newText = "{";

        System.IO.StreamReader file =  new System.IO.StreamReader("c:\\test.txt");

        while ((line = file.ReadLine()) != null)
        {
            newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";

        }
        file.Close();

        newText = newText.Remove(newText.Length -1);
        newText = newText.Replace("=", ":");
        newText += "}";


        Person Person = JsonConvert.DeserializeObject<Person>(newText);

        Console.ReadLine();
    }
}

この助けを願っています。

于 2012-12-04T18:43:24.260 に答える
0

他の人が述べたように、いくつかのオプションがあります。

  1. ファイルをロードし、XMLSerializer/DataContract で動作する XML に変換します
  2. IFormatter を使用して独自のシリアライザーを作成する
  3. ファイルを読み取って手動で実行します(以下の例)

    class Person { public int ID { get; 設定; } パブリック文字列 FamilyName { get; 設定; }

    public Person(string file)
    {
        if (!File.Exists(file))
            return;
    
        string[] personData = File.ReadAllLines(file);
    
        foreach (var item in personData)
        {
            string[] itemPair = item.Split('='); //do some error checking here to see if = isn't appearing twice
            string itemKey = itemPair[0];
            string itemValue = itemPair[1];
            switch (itemKey)
            {
                case "familyName":
                    this.FamilyName = itemValue;
                    break;
                case "id":
                    this.ID = int.Parse(itemValue);
                    break;
                default:
                    break;
            }
    
        }
    }
    

    }

于 2012-12-04T18:12:36.403 に答える