2013-12-11 58 views
0

如何在儘可能少的代碼行中獲取房產的名稱?獲取屬性名稱的最簡單方法?

myClass.FirstName.ToString()輸出該值。

如何使用此屬性獲取字符串值FirstName

+1

爲什麼?你想做什麼?表達式樹可以幫助。 – SLaks

+0

我想將屬性名稱/值添加到字典 4thSpace

+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(); 
     } 
    } 
} 
+0

如何得到它,所以鍵/值匹配?現在,你只有鑰匙。 – 4thSpace

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)。