0
A
回答
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)。
相關問題
- 1. 的Javascript:獲取單個屬性名稱
- 2. 如何獲取表單名稱屬性
- 3. 獲取美國州名單的最簡單方法
- 4. AutoMapperMappingException獲取屬性名稱
- 5. 未獲取名稱屬性
- 6. 獲取屬性名稱
- 7. 獲取屬性名稱
- 8. ssoadm獲得openam屬性名稱的最快方法
- 9. .NET:獲取屬性名稱屬性
- 10. 按屬性值獲取屬性名稱
- 11. 使用屬性讀取XML的最簡單方法
- 12. 獲取CPAN中包裝名稱的完整列表的最簡單方法?
- 13. 從城市名稱獲取郵政編碼的最簡單的方法
- 14. 獲取方法名稱,其中屬性被訪問並讀取屬性值
- 15. Scala要麼:最簡單的方法來獲取左右存在的屬性
- 16. 在C中獲取屬性設置器的方法名稱#
- 17. 使用ASP.NET ClientServerManager方法或屬性獲取控件的名稱
- 18. C#屬性,簡單的方法獲取已標記屬性的值
- 19. 獲取域名IP地址的最簡單方法是什麼?
- 20. 在屬性方法中使用屬性的類中獲取類名稱
- 21. 從方法參數獲取原始屬性名稱
- 22. 如何獲取ZipEntry的簡單名稱?
- 23. 獲取屬性名稱的默認值
- 24. Java獲取名稱屬性的值
- 25. 按名稱獲取ActiveRecord的屬性
- 26. 如何獲取屬性列的名稱?
- 27. 獲取選擇框的名稱屬性
- 28. 獲取C#中的屬性名稱
- 29. NHibernate ClassMap獲取屬性的列名稱
- 30. 獲取值來自的屬性名稱
爲什麼?你想做什麼?表達式樹可以幫助。 – SLaks
我想將屬性名稱/值添加到字典 –
4thSpace
看到我的修改答案。 –