繁体   English   中英

使用反射从对象获取属性和字段值并使用字符串指定字段名称

[英]Getting property and field values from an object using reflection and using a string to specify the field name

好的,所以我正在使用我在堆栈溢出中找到的下面的代码。

    // so we can use reflection to access the object properties
    public static Object GetPropValue(this Object obj, String name)
    {
        foreach (String part in name.Split('.'))
        {
            if (obj == null) { return null; }

            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(part);
            if (info == null) { return null; }

            obj = info.GetValue(obj, null);
        }
        return obj;
    }

    // so we can use reflection to access the object properties
    public static T GetPropValue<T>(this Object obj, String name)
    {
        Object retval = GetPropValue(obj, name);
        if (retval == null) { return default(T); }

        // throws InvalidCastException if types are incompatible
        return (T)retval;
    }

使用字符串访问对象属性。 所以我可以做这样的事情:

DateTime.Now.GetPropValue<int>("TimeOfDay.Hours")

效果很好。

但是我的类不使用属性,我无法更改它,因为它是库的一部分。 我相信它使用的是字段而不是属性(直到现在我不知道它们在 C# 中被区别对待!)。 如何一起访问这些字段名称和属性? 即对于这样的班级

class Student
{
   public string name {get; set;}  // property
   public int age; // field
   public Info info; // field
}

class Info
{
   string a {get; set;} // property
   string b; // fields
}

所以我可以使用这个访问:

Student a;
a.GetPropValue<string>("info.a");  // getting a property which works!
a.GetPropValue<string>("info.b");  // getting a field value which doesn't!

我一发布这个我就想通了,这是我修改后的功能

    // so we can use reflection to access the object properties
    public static Object GetPropertyOrFieldValue(this Object obj, String name)
    {
        foreach (String part in name.Split('.'))
        {
            if (obj == null) { return null; }

            Type type = obj.GetType();
            PropertyInfo propertyInfo = type.GetProperty(part);
            FieldInfo fieldInfo = type.GetField(part);
            if (propertyInfo == null && fieldInfo == null) 
            { 
                return null; 
            }

            obj = (propertyInfo != null) ? propertyInfo.GetValue(obj, null) : (fieldInfo != null) ? fieldInfo.GetValue(obj) : null;
        }
        return obj;
    }

    // so we can use reflection to access the object properties
    public static T GetPropertyOrFieldValue<T>(this Object obj, String name)
    {
        Object retval = GetPropertyOrFieldValue(obj, name);
        if (retval == null) { return default(T); }

        // throws InvalidCastException if types are incompatible
        return (T)retval;
    }

这是获取字段而不是属性的等效代码:

public Object GetFieldValue(this Object obj, String name)
{
    foreach (String part in name.Split('.'))
    {
        if (obj == null) { return null; }

        Type type = obj.GetType();
        FieldInfo info = type.GetField(part);
        if (info == null) { return null; }

        obj = info.GetValue(obj);
    }
    return obj;
}

// so we can use reflection to access the object properties
public T GetFieldValue<T>(this Object obj, String name)
{
    Object retval = GetFieldValue(obj, name);
    if (retval == null) { return default(T); }

    // throws InvalidCastException if types are incompatible
    return (T)retval;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM