C
C#2y ago
SWEETPONY

✅ How to get values of list using reflection?

Recently I wrote some logic to get values from enum:
private JArray GetOptions( Type type, CultureInfo culture )
{
var fields = type.IsEnum
? type.GetFields(BindingFlags.Public | BindingFlags.Static)
: type.GetGenericArguments()[0]
.GetFields(BindingFlags.Public | BindingFlags.Static);

return new JArray(fields.Select(field =>
{
var value = field.GetCustomAttribute<EnumMemberAttribute>()
?.Value ?? field.Name;

var label =
field.GetCustomAttribute<TitleLocalizedAttribute>()
?.ValueGet(field.Name, culture)
?? field.GetCustomAttribute<TitleAttribute>()?.Title
?? field.Name;

return new JObject
{
["label"] = label,
["value"] = value
};
} ) );
}
private JArray GetOptions( Type type, CultureInfo culture )
{
var fields = type.IsEnum
? type.GetFields(BindingFlags.Public | BindingFlags.Static)
: type.GetGenericArguments()[0]
.GetFields(BindingFlags.Public | BindingFlags.Static);

return new JArray(fields.Select(field =>
{
var value = field.GetCustomAttribute<EnumMemberAttribute>()
?.Value ?? field.Name;

var label =
field.GetCustomAttribute<TitleLocalizedAttribute>()
?.ValueGet(field.Name, culture)
?? field.GetCustomAttribute<TitleAttribute>()?.Title
?? field.Name;

return new JObject
{
["label"] = label,
["value"] = value
};
} ) );
}
everything works good, I can work with IEnumerable<T> or just T where T is Enum but now I need to get values from IEnumerable<string> or string: List<string> str {get;set;} = new() {"a","b"}; so I wanna fill Jarray with a and b. How can I do it? I apologize for the question posted. I'm just tired and not thinking straight
6 Replies
reflectronic
reflectronic2y ago
foreach (var str in list)
SWEETPONY
SWEETPONYOP2y ago
what all I have is type
reflectronic
reflectronic2y ago
i don’t understand. a type doesn’t have values you need a list object where else do the values in the list come from
SWEETPONY
SWEETPONYOP2y ago
ok, I will describe my task I have property with custom attribute
[UIControlCatalog]
public List<string> Items {get;set;} = new() {"a", "b", "c"};
[UIControlCatalog]
public List<string> Items {get;set;} = new() {"a", "b", "c"};
UIControlCatalog:
public class UIControlCatalogAttribute
: UIControlAttribute
{
public override void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
var isEnumerable = property.PropertyType.IsEnumerable();

schemaItem.ParsecMetadata ??= new();
schemaItem.ParsecMetadata.UIControlInputType = UIControlInputType.Catalog;

schemaItem.ParsecMetadata.ComponentProperties = new()
{
["multiple"] = isEnumerable,
["options"] = // place for items in list
};
}
}
UIControlCatalog:
public class UIControlCatalogAttribute
: UIControlAttribute
{
public override void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
var isEnumerable = property.PropertyType.IsEnumerable();

schemaItem.ParsecMetadata ??= new();
schemaItem.ParsecMetadata.UIControlInputType = UIControlInputType.Catalog;

schemaItem.ParsecMetadata.ComponentProperties = new()
{
["multiple"] = isEnumerable,
["options"] = // place for items in list
};
}
}
so I need to get values from Items and add it to ["options"] that's what I need
reflectronic
reflectronic2y ago
is it always an IEnumerable<string>? and schemaItem is this, right? so you would get the value of the property using reflection, then cast it to IEnumerable<string>, then do foreach
SWEETPONY
SWEETPONYOP2y ago
hm I think I can't get value of property using reflection I just have PropertyInfo
Want results from more Discord servers?
Add your server