获取房产名称的最简单方法?

时间:2013-12-11 19:12:11

标签: .net c#-4.0

如何在尽可能少的代码行中获取属性的名称?

myClass.FirstName.ToString()输出值。

如何使用此属性获取字符串值FirstName

2 个答案:

答案 0 :(得分:3)

这里有什么问题?是的当然可以。

例如,这是您的实施。

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

namespace PropertiesImpConsoleApp
{
    class Student
    {
        //Declare variables
        string firstname;
        string lastname;

        //Define property for the variables
        public string FirstName
        {
            get
            {
                return firstname;
            }
            set
            {
                firstname = value;
            }
        }
        public string LastName
        {
            get
            {
                return lastname;
            }
            set
            {
                lastname = value;
            }
        }
    }
    class MyMain
    {
        public static void Main(string[] args)
        {
            Student aStudent = new Student();
            Console.WriteLine("Enter First Name");
            aStudent.FirstName = Console.ReadLine();
            Console.WriteLine("Enter LastName");
            aStudent.LastName = Console.ReadLine();

            //And to get the properties names you can do like this
            Dictionary<string, string> aDictionary = new Dictionary<string, string>();

            PropertyInfo[] allproperties = aStudent.GetType().GetProperties().ToArray();
            foreach (var aProp in allproperties)
            {
                aDictionary.Add(aProp.Name, aProp.GetValue(aStudent, null).ToString());
            }

            foreach (KeyValuePair<string, string> pair in aDictionary)
            {
                Console.WriteLine("{0}, {1}",
                pair.Key,
                pair.Value);
            }
            Console.ReadLine();
        }
    }
}

这是使用LINQ表达式的另一个答案。

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

namespace PropertiesImpConsoleApp2
{
    public static class PropertySupport
    {
        public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression == null)
            {
                throw new ArgumentException("", "propertyExpression");
            }
            return memberExpression.Member.Name;
        }
    }
    public class MyClass
    {
        public string PropertyOne { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass aMyClass = new MyClass();
            aMyClass.PropertyOne = "Hello";
            Console.WriteLine(PropertySupport.ExtractPropertyName(() => aMyClass.PropertyOne) + " : " + aMyClass.PropertyOne);
            Console.ReadKey();
        }
    }
}

答案 1 :(得分:2)

void Main()
{
  var aType = new { test = "a", test2 = "b" };

  PropertyInfo[] pInfo = aType.GetType().GetProperties();

  foreach(var p in pInfo)
    Console.WriteLine(p.Name);

}

这将打印

 test
 test2

如果你想要一个属性字典,那么你应该将它转换为动态,这是一个具有此功能的对象。

或许您想要使用动态启动? (有时称为ExpandoObject)。