6

我知道我可以有一个属性,但这比我想做的工作要多……而且不够笼统。

我想做类似的事情

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..

我希望值等于“TestProp”

但我一辈子都找不到正确的反射方法来编写 GetName 方法......

编辑:我为什么要这样做?我有一个类来存储从“名称”、“值”表中读取的设置。这是由基于反射的通用方法填充的。我很想写反...

/// <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);
            }
        }
    }

进一步编辑:我只是在代码中,有一个 Whotsit 实例,我想获取“TestProp”属性的文本字符串。我知道这似乎有点奇怪,我可以只使用文字“TestProp” - 或者在我的类到数据表函数的情况下,我会在 PropertyInfos 的 foreach 循环中。我只是好奇而已...

原始代码有字符串常量,我发现它很笨拙。

4

7 回答 7

6

不,没有什么可做的。该表达式whotsit.TestProp将评估该属性。您想要的是神话般的“infoof”运算符:

// I wish...
MemberInfo member = infoof(whotsit.TestProp);

事实上,您只能使用反射来按名称获取属性 - 而不是从代码中获取。(当然,或者获取所有属性。但它仍然对您的样本没有帮助。)

一种替代方法是使用表达式树:

Expression<Func<string>> = () => whotsit.TestProp;

然后检查表达式树以获取属性。

如果这些都没有帮助,也许您可​​以告诉我们更多关于您为什么需要此功能的信息?

于 2008-12-23T12:45:32.613 回答
3

这是可能的(没有反射),但仅限于最新的 C# 3.0

快速且非常肮脏

class Program
{
    static void Main()
    {
        string propertyName = GetName(() => AppDomain.CurrentDomain);
        Console.WriteLine(propertyName); // prints "CurrentDomain"
        Console.ReadLine();
    }

    public static string GetName(Expression<Func<object>> property)
    {
        return property.Body.ToString().Split('.').Last();
    }
}

更新:我刚刚意识到Jon Skeet(有人惊讶吗?:) 已经涵盖了这种可能性,但我会在这里保留我的答案,以防有人对某个示例感兴趣。

于 2008-12-23T13:11:58.380 回答
0

你试图做的事情是不可能的。使用反射,您可以:

  • 从字符串名称中获取属性、字段或方法的详细信息。
  • 获取给定类型的所有属性、字段或方法的列表。

您的 GetName 方法在调用时会传递一个字符串。GetMethod 将知道该字符串,但不保留源属性元数据。

出于兴趣,你为什么要这样做?

于 2008-12-23T12:47:29.490 回答
0

我认为这是不可能的,唯一的方法是迭代属性:

class TestClass
{
    private string _field;

    public string MyProperty
    {
        get { return _field; }
    }
}
class Program
{
    static void Main(string[] args)
    {
        TestClass test = new TestClass();
        PropertyInfo[] info = test.GetType().GetProperties();
        foreach(PropertyInfo i in info)
            Console.WriteLine(i.Name);
        Console.Read();
    }
}
于 2008-12-23T12:53:03.363 回答
0

Kpollack,你在之前的评论中说:

这仍然不会让我能够从它的实例中获取属性的名称。

这使我相信您以某种方式引用了属性。你是怎么得到这个参考的?它的类型是什么?你能提供一个代码示例吗?如果它是一个 PropertyInfo 对象,你已经拥有了你需要的东西;由于情况似乎并非如此,我们正在处理其他事情,我很想看看你必须处理的是什么

PS 请原谅我看起来迟钝:现在还早,我还没有喝足够的咖啡,而且我面前没有我的 IDE。:-/

于 2008-12-23T13:30:03.740 回答
0

仅供参考,我尝试对其进行序列化,以查看是否偶然包含属性名称,但没有运气。

下面的非工作代码:

Whotsit w = new Whotsit();
XmlSerializer xs = new XmlSerializer(w.TestProp.GetType());
TextWriter sw = new StreamWriter(@"c:\TestProp.xml");
xs.Serialize(sw, w.TestProp);
sw.Close();
于 2008-12-23T13:35:56.133 回答
-1

Type 类的GetProperties将为您提供该类型的属性列表。

Type t = whotsit.GetType();
PropertyInfo[] pis = t.GetProperties();
于 2008-12-23T12:44:29.387 回答