Tôi biết tôi có thể có một thuộc tính nhưng đó là công việc nhiều hơn tôi muốn đi ... và không đủ chung.Làm cách nào để lấy tên của một tài sản từ một tài sản trong C# (2.0)
tôi muốn làm một cái gì đó giống như
class Whotsit
{
private string testProp = "thingy";
public string TestProp
{
get { return testProp; }
set { testProp = value; }
}
}
...
Whotsit whotsit = new Whotsit();
string value = GetName(whotsit.TestProp); //precise syntax up for grabs..
nơi tôi mong đợi giá trị tương đương với "TestProp"
nhưng tôi không thể cho cuộc đời của tôi tìm ra phương pháp phản ánh đúng để viết Phương thức GetName ...
EDIT: Tại sao tôi muốn làm điều này? Tôi có một lớp để lưu trữ các cài đặt được đọc từ bảng 'tên', 'giá trị'. Điều này được điền bởi một phương pháp tổng quát dựa trên sự phản chiếu. Tôi rất muốn viết ngược lại ...
/// <summary>
/// Populates an object from a datatable where the rows have columns called NameField and ValueField.
/// If the property with the 'name' exists, and is not read-only, it is populated from the
/// valueField. Any other columns in the dataTable are ignored. If there is no property called
/// nameField it is ignored. Any properties of the object not found in the data table retain their
/// original values.
/// </summary>
/// <typeparam name="T">Type of the object to be populated.</typeparam>
/// <param name="toBePopulated">The object to be populated</param>
/// <param name="dataTable">'name, 'value' Data table to populate the object from.</param>
/// <param name="nameField">Field name of the 'name' field'.</param>
/// <param name="valueField">Field name of the 'value' field.</param>
/// <param name="options">Setting to control conversions - e.g. nulls as empty strings.</param>
public static void PopulateFromNameValueDataTable<T>
(T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)
{
Type type = typeof(T);
bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;
foreach (DataRow dataRow in dataTable.Rows)
{
string name = dataRow[nameField].ToString();
System.Reflection.PropertyInfo property = type.GetProperty(name);
object value = dataRow[valueField];
if (property != null)
{
Type propertyType = property.PropertyType;
if (nullStringsAsEmptyString && (propertyType == typeof(String)))
{
value = TypeHelper.EmptyStringIfNull(value);
}
else
{
value = TypeHelper.DefaultIfNull(value, propertyType);
}
property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);
}
}
}
THÊM EDIT: Tôi vừa mới viết mã, có một trường hợp Whotsit và tôi muốn lấy chuỗi văn bản của thuộc tính 'TestProp'. Nó có vẻ kỳ lạ mà tôi biết, tôi chỉ có thể sử dụng "TestProp" theo nghĩa đen - hoặc trong trường hợp lớp của tôi với hàm datatable, tôi sẽ ở trong vòng lặp foreach của PropertyInfos. Tôi chỉ tò mò ...
Mã gốc có các hằng số chuỗi, mà tôi thấy vụng về.
Xem http://stackoverflow.com/questions/855337/referring-to-the-property-itself-in-c-reflection-generic-type –
Bạn có thể có một câu trả lời ở đây http://stackoverflow.com/a/1225702/661933 – nawfal