❔ ✅ How to get attributes values?
I have following property with custom attribute
So my question is it possible to get EnumMember value and TitleLocalized value from EventCategoryType in custom attribute?
[CatalogComponent]
:
[CatalogComponent]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
[CatalogComponent]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
EventCategoryType
is enum:
public enum EventCategoryType
{
[EnumMember( Value = "undefined" )]
Undefined,
[EnumMember( Value = "diagnostics" )]
[TitleLocalized( typeof( Resources.EventCategoryTypeTitle ) )]
[DescriptionLocalized( typeof( Resources.EventCategoryTypeDescription ) )]
Diagnostics,
[EnumMember( Value = "warning" )]
[TitleLocalized( typeof( Resources.EventCategoryTypeTitle ) )]
[DescriptionLocalized( typeof( Resources.EventCategoryTypeDescription ) )]
Warning
}
public enum EventCategoryType
{
[EnumMember( Value = "undefined" )]
Undefined,
[EnumMember( Value = "diagnostics" )]
[TitleLocalized( typeof( Resources.EventCategoryTypeTitle ) )]
[DescriptionLocalized( typeof( Resources.EventCategoryTypeDescription ) )]
Diagnostics,
[EnumMember( Value = "warning" )]
[TitleLocalized( typeof( Resources.EventCategoryTypeTitle ) )]
[DescriptionLocalized( typeof( Resources.EventCategoryTypeDescription ) )]
Warning
}
11 Replies
using reflection, sure
okay, let's see
we have this property:
how can I get T in CatalogComponent?
[CatalogComponent]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<T>? EventCategories { get; set; }
[CatalogComponent]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<T>? EventCategories { get; set; }
wait is it generic or is it EventCategoryTypes
[CatalogComponent]
public HashSet<A>? EventCategories { get; set; }
[CatalogComponent]
public HashSet<B>? EventCategories { get; set; }
[CatalogComponent]
public HashSet<C>? EventCategories { get; set; }
[CatalogComponent]
public HashSet<A>? EventCategories { get; set; }
[CatalogComponent]
public HashSet<B>? EventCategories { get; set; }
[CatalogComponent]
public HashSet<C>? EventCategories { get; set; }
anyway to get T you look into GetType()
probably it would be GenericTypeArgument
[CatalogComponent<EventCategoryType>]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
public class CatalogComponentAttribute<T> : Attribute
{
}
[CatalogComponent<EventCategoryType>]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
public class CatalogComponentAttribute<T> : Attribute
{
}
for the attributes, you have to search into Field for GetCustomAttribute
hm I will try
smth like that..?
is it good solution?
static private string GetEnumMemberValue()
=> typeof(T).GetCustomAttribute<EnumMemberAttribute>()?.Value;
static private string GetEnumMemberValue()
=> typeof(T).GetCustomAttribute<EnumMemberAttribute>()?.Value;
[CatalogComponent<EventCategoryType>]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
public class CatalogComponentAttribute<T> : Attribute
{
public InputType InputType =>
InputType.Catalog;
public string? EnumMemberValue =>
typeof(T).GetCustomAttribute<EnumMemberAttribute>()?
.Value;
public string? TitleLocalizedValue =>
typeof(T).GetCustomAttribute<TitleLocalizedAttribute>()?
.Value;
}
[CatalogComponent<EventCategoryType>]
[JsonProperty( PropertyName = "event_categories" )]
public HashSet<EventCategoryType>? EventCategories { get; set; }
public class CatalogComponentAttribute<T> : Attribute
{
public InputType InputType =>
InputType.Catalog;
public string? EnumMemberValue =>
typeof(T).GetCustomAttribute<EnumMemberAttribute>()?
.Value;
public string? TitleLocalizedValue =>
typeof(T).GetCustomAttribute<TitleLocalizedAttribute>()?
.Value;
}
this is, generically, the part that would check the attribute of an enum
one way to do it
so there's a step in between, GetField
sadly it's not the easiest thing to do
anyway you can inspect GetType() and look around for what you need
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)] class TestAttribute : Attribute { }
enum TestEnum { [Test]TestValue }
var enumValue = TestEnum.TestValue;
var valueType = enumValue.GetType();
string valueName = enumValue.ToString();
var valueField = valueType.GetField(valueName);
var valueAttr = valueField.GetCustomAttribute(typeof(TestAttribute), false);
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)] class TestAttribute : Attribute { }
enum TestEnum { [Test]TestValue }
var enumValue = TestEnum.TestValue;
var valueType = enumValue.GetType();
string valueName = enumValue.ToString();
var valueField = valueType.GetField(valueName);
var valueAttr = valueField.GetCustomAttribute(typeof(TestAttribute), false);
okay, I see
I did this and it works well:
public class CatalogComponentAttribute<T> : Attribute
{
public InputType InputType =>
InputType.Catalog;
public IEnumerable<(string? title, string? enumMember)> LocalizedValues =>
typeof(T)
.GetProperties()
.Select(property => (
title: property.GetCustomAttribute<TitleLocalizedAttribute>()?.Value,
enumMember: property.GetCustomAttribute<EnumMemberAttribute>()?.Value
));
}
public class CatalogComponentAttribute<T> : Attribute
{
public InputType InputType =>
InputType.Catalog;
public IEnumerable<(string? title, string? enumMember)> LocalizedValues =>
typeof(T)
.GetProperties()
.Select(property => (
title: property.GetCustomAttribute<TitleLocalizedAttribute>()?.Value,
enumMember: property.GetCustomAttribute<EnumMemberAttribute>()?.Value
));
}
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.