在下拉列表中显示属性名称

时间:2013-08-27 14:06:55

标签: c#

让我说我有这个:

public class Languages
{
    public string Language;
    public string SpokenAbility;
    public string WrittenAbility;
}

有没有办法可以将其加载到下拉列表中,以便下拉列表显示项目:Language,SpokenAbility和WrittenAbility?

2 个答案:

答案 0 :(得分:5)

// using System.Reflection;
// using System.Linq;

IEnumerable<String> properties = typeof(Languages)
    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
    .Select(x => x.Name);

您可以使用反射来获取属性,使用LINQ使其更容易。

Spontifixus指出你正在使用田地。所有需要切换的都是.GetProperties.GetFields

IEnumerable<String> fields = typeof(Languages)
    .GetFields(BindingFlags.Public | BindingFlags.Instance)
    .Select(x => x.Name);

扩展方法,使其更容易:

public static class FieldAndPropertyExtensions
{
    /*
     * Field Methods
     */

    public static IEnumerable<String> GetFields<T>(this T obj, Boolean includeInheritedFields = true) where T : class
    {
        return getFieldsFor<T>(includeInheritedFields).Select(x => x.Name);
    }
    public static IEnumerable<String> GetFieldsFor<T>(Boolean includeInheritedFields = true) where T : class
    {
        return getFieldsFor<T>(includeInheritedFields).Select(x => x.Name);
    }
    public static IDictionary<String, Object> GetFieldValueDictionary<T>(this T obj, Boolean includeInheritedFields = true) where T : class
    {
        IEnumerable<FieldInfo> fields = getFieldsFor<T>(includeInheritedFields);

        IDictionary<String, Object> result = new Dictionary<String, Object>();
        foreach (var field in fields)
        {
            result.Add(field.Name, field.GetValue(obj));
        }
        return result;
    }

    /*
     * Property Methods
     */

    public static IEnumerable<String> GetProperties<T>(this T obj, Boolean includeInheritedProperties = true) where T : class
    {
        return getPropertiesFor<T>(includeInheritedProperties).Select(x => x.Name);
    }
    public static IEnumerable<String> GetPropertiesFor<T>(Boolean includeInheritedProperties = true) where T : class
    {
        return getPropertiesFor<T>(includeInheritedProperties).Select(x => x.Name);
    }
    public static IDictionary<String, Object> GetPropertyValueDictionary<T>(this T obj, Boolean includeInheritedProperties = true) where T : class
    {
        IEnumerable<PropertyInfo> properties = getPropertiesFor<T>(includeInheritedProperties);

        IDictionary<String, Object> result = new Dictionary<String, Object>();
        foreach (var property in properties)
        {
            result.Add(property.Name, property.GetValue(obj));
        }
        return result;
    }

    /*
     * Helper methods
     */
    private static IEnumerable<FieldInfo> getFieldsFor<T>(Boolean includeInheritedFields = true) where T : class
    {
        return typeof(T)
            .GetFields(BindingFlags.Public | BindingFlags.Instance)
            .Where(x => includeInheritedFields || x.DeclaringType == typeof(T));
    }
    private static IEnumerable<PropertyInfo> getPropertiesFor<T>(Boolean includeInheritedFields = true) where T : class
    {
        return typeof(T)
            .GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .Where(x => includeInheritedFields || x.DeclaringType == typeof(T));
    }
}

使用示例:

// instance methods:
var languages = new Languages();
var properties = languages.GetProperties(); // prop1,prop2,prop3
var fields = languages.GetFields(); // field1,field2,field3

var propAndValue = languages.GetPropertyValueDictionary(); // Dict<propertyName,value>
var fieldAndValue = languages.GetFieldValueDictionary(); // Dict<fieldName,value>

// non-instance methods:
var properties = ObjectExtensions.GetPropertiesFor<Languages>(); // prop1,prop2,prop3
var fields = ObjectExtensions.GetFieldsFor<Languages>(); // field1,field2,field3

答案 1 :(得分:1)

首先,您需要确保您的班级具有属性。您问题中定义的是字段。要将它们转换为属性,只需添加get;set; - 方法:

public class Languages
{
    public string Language { get; set; }
    public string SpokenAbility { get; set; }
    public string WrittenAbility {get; set; }
}

然后,您可以使用以下代码列出属性:

var properties = typeof(Languages).GetProperties().Select(p => p.Name)

要检索属性的值,请使用以下代码:

var language = new Languages(){ Language="German" };
var result = typeof(Languages).GetProperty("Language").GetValue(language);