C#为每个循环调用所有属性

时间:2015-04-08 12:44:35

标签: c# properties foreach

我有一个foreach循环,我想在foreach循环中调用某个类的所有属性,所以我不必全部写出来。

我创建的课程

public Person()
    {
        firstname = "";
        surname = "";
        haircolor = "";
        eyecolor = "";
        weight = 0;
        height = 0;
        age = 0;
    }

这是我试图压缩的代码

  Console.WriteLine("Please enter the next persons firstname");//new person user input (firstname)
  addperson.firstname = Console.ReadLine();

  Console.WriteLine("Enter the persons surname");//surname
  addperson.surname = Console.ReadLine();

  Console.WriteLine("Enter " + addperson.name + "'s hair color");//hair color
  addperson.haircolor = Console.ReadLine();

  Console.WriteLine("Enter the age of " + addperson.firstname);//age
  addperson.age = Convert.ToInt32(Console.ReadLine());

  Console.WriteLine("Enter the weight of " + addperson.firstname);//weight
  addperson.weight = Convert.ToDouble(Console.ReadLine());

  Console.WriteLine("Enter the height of " + addperson.firstname);//height
  addperson.height = Convert.ToDouble(Console.ReadLine());

我已经开始使用foreach循环,我想要一种方法将所有代码压缩成循环

  foreach (Person.)
            {
                Console.WriteLine("Please enter " +addperson.ToString);
                  Person.addperson = Console.ReadLine();
            }

非常感谢任何帮助

3 个答案:

答案 0 :(得分:2)

你需要使用反射来动态地遍历每个属性,个人我不会改变它以使用反射,因为反射有性能损失,但这里是代码供你参考:

将类字段更改为属性:

    public class Person
    {
      public string firstname {get;set;}
      public string surname {get;set;}
      public string haircolor {get;set;}
      public string eyecolor {get;set;}
      public string weight {get;set;}
      public string height {get;set;}
      public string age {get;set;}
     }

在主方法中写下此代码:

       Person addperson = new Person();
       PropertyInfo[] props = addperson.GetType().GetProperties();

        foreach(PropertyInfo prop in props)
        {
              Console.WriteLine("Please enter " + prop.Name.ToString());
              string input = Console.ReadLine();
              prop.SetValue(addperson, input, null);
        }

修改

这行代码:

 PropertyInfo[] props = addperson.GetType().GetProperties();

返回Person类型(类)的所有公共属性,然后每个PropertyInfo对象发现属性的属性并提供对其元数据的访问。

答案 1 :(得分:0)

我这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace YourNameSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = GetPersonFromUserInput();
        }

        private static Person GetPersonFromUserInput()
        {
            Person p = new Person();
            Type t = typeof(Person);
            foreach (PropertyInfo pi in t.GetProperties().Where(pi => pi.GetCustomAttribute<PromptAttribute>() != null))
            {
                PromptAttribute attribute = pi.GetCustomAttribute<PromptAttribute>();

                Console.Write("{0}: ", pi.GetCustomAttribute<PromptAttribute>().Prompt);

                if (pi.PropertyType == typeof(int))
                {
                    PromptInteger(p, pi);
                }
                else if (pi.PropertyType == typeof(string))
                {
                    PromptString(p, pi);

                } //add more types in this manner                           
            }

            return p;
        }

        private static void PromptString(Person p, PropertyInfo pi)
        {
            string userInput = Console.ReadLine();
            pi.SetMethod.Invoke(p, new object[] { userInput });
        }

        private static void PromptInteger(Person p, PropertyInfo pi)
        {
            int userInput;
            while (!int.TryParse(Console.ReadLine(), out userInput))
            {
                Console.Write("You have to enter an integer: ");
            }

            pi.SetMethod.Invoke(p, new object[] { userInput });
        }


    }

    public class Person
    {
        [Prompt("Please enter the persons firstname")]
        public string FirstName { get; set; }

        [Prompt("Please enter the persons surname")]
        public string SurName { get; set; }

        [Prompt("Please enter the persons haircolor")]
        public string HairColor { get; set; }

        [Prompt("Please enter the persons eyecolor")]
        public string EyeColor { get; set; }

        [Prompt("Please enter the persons weight")]
        public int Weight { get; set; }

        [Prompt("Please enter the persons height")]
        public int Height { get; set; }

        [Prompt("Please enter the persons age")]
        public int Age { get; set; }
    }

    public class PromptAttribute : Attribute
    {
        public string Prompt { get; private set; }

        public PromptAttribute(string prompt)
        {
            Prompt = prompt;
        }
    }
}

使用custom - 属性可以定义提示。如果您有更多类型,只需在循环中添加更多其他if。

答案 2 :(得分:0)

一个简单的控制台应用程序,使用反射,linq和字典:

使用陈述:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

你的班级:

public class Person
{
  public string  firstname {get; set;}
  public string surname { get; set; }
  public string haircolor { get; set; }
  public string eyecolor { get; set; }
  public string weight { get; set; }
  public string height { get; set; }
  public string age { get; set; }
}

控制台应用程序:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var per = new Person();
            var perDict = new Dictionary<string, string>();


            foreach (
                var c in 
                    per.GetType()
                        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                            .ToDictionary(prop => prop.Name, prop => prop.GetValue(per,null))
                        )
            {

            Console.Write("Greetings, Please Enter Your Value for: " + c.Key + " ");
            var answer = Console.ReadLine();
            perDict.Add(c.Key, answer);
            per.GetType().GetProperty(c.Key).SetValue(per,answer, null);
            }
        }
    }

}