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

本文关键字:属性 显示 下拉列表 | 更新日期: 2023-09-27 18:29:07

假设我有这个:

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

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

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

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

首先,您需要确保您的类具有属性。问题中定义的是字段。要将它们转换为属性,只需添加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);