gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:03 24 4
gpt4 key购买 nike

假设我有这个:

public class Languages
{
public string Language;
public string SpokenAbility;
public string 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

关于c# - 在下拉列表中显示属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com