如何在C#中比较解析字典中的对象

时间:2013-05-13 01:09:58

标签: c# dictionary

所以我有一个Person类,其中包含info (firstName, lastName, mother, father, siblings, spouse),我想将Person类的人添加到Dictionary中。我想比较解析字典以确定对象的关系(即给定一个人,找到他们的堂兄弟,兄弟姐妹等)。我的问题有两个: 1)我应该如何设置我的词典< ...> 2)如何访问列表中每个人的属性?我第一次尝试:

Dictionary<string, object> dictionary = new Dictionary<string, object>();  
var human = Person.AddPerson();  // Person.AddPerson() returns a new instance of a Person                               
dictionary.Add(human.name, human)    // setting the key to full name, value to Person object.

我应该尝试像Dictionary<string, string> <firstName, lastName>这样的东西,一旦我得到同名人物的所有比赛,然后开始在字典中搜索母亲,父亲等等。这似乎非常缓慢而且不是正确的方法 编辑:这是我的Person类和其他类之一(记住我只是设置它,我将处理所有用户输入,等等):

public class Person
{
    public string name { get; set;}
    public Mother mother { get; set; } 
    public Father father { get; set; }
    public Spouse spouse { get; set; }
    public Sibling sibling { get; set; }

    //List<Sibling> siblings = new List<Sibling>();

    public Person()
    { }

    public static Person AddPerson()
    {
        Person newPerson = new Person();
        Console.WriteLine("Enter name:");
        newPerson.name = Console.ReadLine().Trim();
        Console.WriteLine("Enter mother's name:");
        string input = Console.ReadLine().Trim();
        Mother mom = new Mother(input);
        newPerson.mother = mom;
        Console.WriteLine("Enter father's name:");
        string input1 = Console.ReadLine().Trim();
        Father dad = new Father(input1);
        newPerson.father = dad;
        Console.WriteLine("Enter sibling's name:");
        string input2 = Console.ReadLine().Trim();
        Sibling sib = new Sibling(input2);
        newPerson.sibling = sib;
        Console.WriteLine("Enter spouse's name:");
        string input3 = Console.ReadLine().Trim();
        Spouse partner = new Spouse(input3);
        newPerson.spouse = partner;

        return newPerson;
    }

}

public class Sibling : Person
{
    private string name;
    public Sibling(string Name)
    {
        name = Name;

    }
    public Sibling()
    { }
}

public class Mother : Person
{
    private string name;
    public Mother(string Name)
    {            
        //Mother mom = new Mother();
        name = Name;
    }
    public Mother()
    { }
}

public class Father : Person
{
    private string name;
    public Father(string Name)
    {
        name = Name;

    }
    public Father()
    { }
}

public class Spouse : Person
{
    private string name;
    public Spouse(string Name)
    {
        name = Name;

    }
    public Spouse()
    { }
}

}

2 个答案:

答案 0 :(得分:1)

重要的是,key的{​​{1}}必须是唯一的。

因此,如果您怀疑自己有多个dictionary具有相同的Person,那么您可能无法仅使用first name作为first name

因此,key可以是key属性的组合,例如Person再次假设此组合是唯一的或某些string.concat(firstName, lastName)(如GUID)。

修改

为了帮助打印,您可以覆盖每个班级的random number。以下是一个例子

ToString

这应该适用:

public class Father : Person
    {
        private string name;
        public Father(string Name)
        {
            name = Name;

        }
        public Father()
        { }

        public override string ToString()
        {
            return this.name;
        }
    }

答案 1 :(得分:-1)

如果您坚持使用Dictionary,则应将字典定义为new Dictionary<string, Person>(),这样就可以像这样循环字典:

foreach (var person in dictionary)
{
    if(person.Value.firstName=="somename")
    // do something
}

。 或者,您可以new Dictionary<string, object>()将[{1}}转换为object,如此

Person

我建议第一种解决方案,第二种方式将需要额外的包装和拆包费用。

编辑: 我的错?