I've the following attribute:
class HandlerAttribute : System.Attribute
{
public string MainName { get; private set; }
public string SubName { get; private set; }
public HandlerAttribute(string pValue, bool pIsMain) {
if (pIsMain) MainName = pValue;
else SubName = pValue;
}
}
And this is the way I use the attribute
[Handler("SomeMainName", true)]
class Class1 {
[Handler("SomeSubName", false)]
void HandleThis() {
Console.WriteLine("Hi");
}
}
What I want to achieve is that I import the MainName value from the parent class attribute into the method defined inside the class.
I hope that someone can help me with this :)
Thanks in advance
I would advice to write a helper method for this, basically it isn't possible by default because you don't have access to the attribute's target.
Code
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class HandlerAttribute : Attribute
{
public string MainName { get; private set; }
public string SubName { get; private set; }
public HandlerAttribute(string pValue, bool pIsMain) {
if (pIsMain) MainName = pValue;
else SubName = pValue;
}
public HandlerAttributeData GetData(MemberInfo info)
{
if (info is Type)
{
return new HandlerAttributeData(MainName, null);
}
HandlerAttribute attribute = (HandlerAttribute)info.DeclaringType.GetCustomAttributes(typeof(HandlerAttribute), true)[0];
return new HandlerAttributeData(attribute.MainName, SubName);
}
}
class HandlerAttributeData
{
public string MainName { get; private set; }
public string SubName { get; private set; }
public HandlerAttributeData(String mainName, String subName)
{
MainName = mainName;
SubName = subName;
}
}
Usage
HandlerAttribute att = (HandlerAttribute)typeof(App).GetCustomAttributes(typeof(HandlerAttribute), true)[0];
HandlerAttributeData data = att.GetData(typeof(App));
You can use it with any member and if you write a extension method you can make it even shorter.
Update:
You may use this extension method as well.
public static class AttributeExtensions
{
public static string GetMainName(this Class1 class1)
{
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(class1.GetType()); // reflection
foreach (System.Attribute attr in attrs)
{
if (attr is HandlerAttribute)
{
string mainName = (attr as HandlerAttribute).MainName;
return mainName;
}
}
throw new Exception("No MainName Attribute");
}
}
Now after coding the extension method you may use it on any Class1 type object;
Class1 c1 = new Class1();
string a =c1.GetMainName();
Or you may use it directly.
public class HandlerAttribute : System.Attribute
{
public string MainName { get; private set; }
public string SubName { get; private set; }
public HandlerAttribute(string pValue, bool pIsMain)
{
if (pIsMain) MainName = pValue;
else SubName = pValue;
}
}
[Handler("SomeMainName", true)]
class Class1
{
[Handler("SomeSubName", false)]
public void HandleThis()
{
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(this.GetType()); // reflection
foreach (System.Attribute attr in attrs)
{
if (attr is HandlerAttribute)
{
string mainName = (attr as HandlerAttribute).MainName;
}
}
}
}
Related
I am writing a method for extracting all properties from an object (including properties of its own) with custom attribute . For example
public class SomeModel
{
[Custom]
public string Name { get; set; }
public string TestData { get; set; }
[Custom]
public string Surname { get; set; }
public InnerModel InnerModel { get; set; }
}
And Inner Model :
public class InnerModel
{
public string Id { get; set; } = "TestID";
[Custom]
public string Year { get; set; }
public ThirdObject HidedObject { get; set; }
}
And the third one :
public class ThirdObject
{
[Custom]
public string HidedName { get; set; }
}
I need to find all properties with "Custom" attribute .
Testing :
SomeModel model = new SomeModel()
{
Name = "farid",
Surname = "Ismayilzada",
TestData = "Test" ,
InnerModel = new InnerModel() { Year ="2022" , HidedObject= New ThirdObject{ HidedName="Secret"}}
};
I need to write the method
GetMyProperties(model) => List<PropInf>()
[PropertyName= Name,Value=Farid ,Route="Name" ]
[PropertyName= Surname,Value=Ismayilzada,Route="Surname" ]
[PropertyName= Year,Value=2022,Route="InnerModel.Year" ]
[PropertyName= HidedName,Value=Secret,Route="InnerModel.HidedObject.HidedName" ]
How to get this information ?
You can write a method like this :
private static IEnumerable<PropInfo> GetPropertiesInfo(object obj, string route = "")
{
List<PropInfo> results = new List<PropInfo>();
// You can filter wich property you want https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo?view=net-6.0
var objectProperties = obj.GetType().GetProperties().Where(p => p.CanRead);
foreach (var property in objectProperties)
{
var value = property.GetValue(obj);
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
{
results.AddRange(GetPropertiesInfo(value, route + property.Name + "."));
}
else
{
// Check if the property has the Custom Attribute
var customAttributes = property.GetCustomAttributes<CustomAttribute>();
if (!customAttributes.Any())
continue;
// You can set a method in your Attribute : customAttributes.First().CheckIfNeedToStoreProperty(obj);
results.Add(new PropInfo()
{
PropertyName = property.Name,
Value = value,
Route = route + property.Name
});
}
}
return results;
}
public class PropInfo
{
public string PropertyName { get; set; }
public object Value { get; set; }
public string Route { get; set; }
}
public class CustomAttribute : Attribute
{
public bool CheckIfNeedToStoreProperty(object obj)
{
return true;
}
}
I want the value to be initialized according to the attribute When Instance is created.
class FieldAttr : Attribute
{
public readonly string key;
public Key_FieldAttr(string key)
{
this.key = key;
}
}
class CtorAttr : Attribute
{
}
static string FromTable(string key)
{
// will return localized string of key from table.
}
...
class LocalizedMetadata
{
[FieldAttr("NAME")]
public string Name { get; }
[FieldAttr("DESC")]
public readonly string Description;
[CtorAttr]
public LocalizedMetadata(string header)
{
// I want "Key" attribute to do as below...
// this.Name = FromTable(header + "NAME");
// this.Description = FromTable(header + "DESC");
}
}
public static void Main(string[] args)
{
var foo = new LocalizedMetadata("UNITY_");
Console.WriteLine(foo.Name); // = FromTable("UNITY_NAME");
Console.WriteLine(foo.Description); // = FromTable("UNITY_DESC");
}
I don't know where to attach what kind of Attribute (Field, Construction, Class) will be possible.
I looked up documents about Attribute and Reflection, but I don't know if this is possible.
I think below solution will solve your problem.
class LocalizedMetadata
{
[FieldAttr("NAME")]
public string Name { get; set; }
[FieldAttr("DESC")]
public string Description { get; set; }
public LocalizedMetadata(string header)
{
foreach (var property in typeof(LocalizedMetadata).GetProperties())
{
foreach (var attr in property.GetCustomAttributes(false))
{
FieldAttr fieldAttr = (FieldAttr)attr;
property.SetValue(this, header + fieldAttr.Key);
}
}
}
}
public static void Main(string[] args)
{
var foo = new LocalizedMetadata("UNITY_");
Console.WriteLine(foo.Name);
Console.WriteLine(foo.Description);
}
Output:
UNITY_NAME
UNITY_DESC
In my following code snippet, I'm not able to understand as to why "customAttribute" value for property "XX" is always null.It works fine for property "X" though.
Any ideas?
Thanks.
System.Reflection.PropertyInfo[] props = myInp.GetProperties();
foreach (var prop in props)
{
if (prop.Name == "XX")
{
var customAttribute = prop.GetCustomAttribute(typeof(CustomAttribute)) as CustomAttribute; //This is always null for XX
}
}
public class MyClass
{
[CustomAttribute(MyProp1 = true, MyProp2="test")]
public bool X{ get ; set;},
[CustomAttribute(MyProp1 = true)]
public MyEnum XX{ get ; set;}
}
public enum MyEnum
{
ABC = 0,
XYZ = 1
}
public class CustomAttribute : Attribute
{
public bool MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
This works just fine for me:
class Program
{
static void Main(string[] args)
{
var myClassProps = typeof(MyClass).GetProperties();
foreach (var prop in myClassProps.Where(p => p.Name == nameof(MyClass.XX)))
{
var attribs = prop.GetCustomAttributes(typeof(CustomAttribute), false);
if (attribs.Length > 0)
{
var customAttrib = attribs[0] as CustomAttribute;
//Do Something with it here
}
}
}
}
public enum MyEnum
{
ABC = 0,
XYZ = 1
}
public class CustomAttribute : Attribute
{
public bool MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
public class MyClass
{
[Custom(MyProp1 = true, MyProp2 = "test")]
public bool X { get; set; }
[Custom(MyProp1 = true)]
public MyEnum XX { get; set; }
}
I'm not sure where you get the PropertyInfo.GetCustomAttribute, I couldn't find that member in PropertyInfo.
I have a DTO that looks like this:
public class MyDto
{
[MyAttribute("attribute1")]
public string Property1 {get;set;}
[MyAttribute("attribute2")]
public string Property2 {get;set;}
}
If I have the string "attribute1", how do I use that to get to the value of Property1 in an instance of MyDto?
Use Reflection. Unfortunately, there's no way to obtain the property from an attribute: you have to iterate through each property and check its attribute.
Not the most robust code, but try this:
public class MyAttributeAttribute : Attribute
{
public MyAttributeAttribute(string value)
{
Value=value;
}
public string Value { get; private set; }
}
public class MyDto
{
[MyAttribute("attribute1")]
public string Property1 { get; set; }
[MyAttribute("attribute2")]
public string Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyDto dto=new MyDto() { Property1="Value1", Property2="Value2" };
string value=GetValueOf<string>(dto, "attribute1");
// value = "Value1"
}
public static T GetValueOf<T>(MyDto dto, string description)
{
if(string.IsNullOrEmpty(description))
{
throw new InvalidOperationException();
}
var props=typeof(MyDto).GetProperties().ToArray();
foreach(var prop in props)
{
var atts=prop.GetCustomAttributes(false);
foreach(var att in atts)
{
if(att is MyAttributeAttribute)
{
string value=(att as MyAttributeAttribute).Value;
if(description.Equals(value))
{
return (T)prop.GetValue(dto, null);
}
}
}
}
return default(T);
}
}
I am creating a custom attribute. And I will use it in multiple classes:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class Display : System.Attribute
{
public string Name { get; set; }
public string Internal { get; set; }
}
public class Class1
{
[Display(Name = "ID")]
public int ID { get; set; }
[Display(Name = "Name")]
public string Title { get; set; }
}
public class Class2
{
[Display(Name = "ID")]
public int ID { get; set; }
[Display(Name = "Name")]
public string Title { get; set; }
}
Here is working correctly but I want to make it as generic as possible, like the MVC example:
Class1 class1 = new Class1();
class1.Title.DisplayName () / / returns the value "Name"
The only thing I could do was generate a loop of my property, but I need to inform my typeof Class1
foreach (var prop in typeof(Class1).GetProperties())
{
var attrs = (Display[])prop.GetCustomAttributes(typeof(Display), false);
foreach (var attr in attrs)
{
Console.WriteLine("{0}: {1}", prop.Name, attr.Name);
}
}
Is there any way to do it the way I want?
You can't do what you've shown exactly because class1.Title is an expression that evaluates to a string. You are looking for metadata about the Title property. You can use Expression Trees to let you write something close. Here's a short helper class that pulls the attribute value out of a property in an expression tree:
public static class PropertyHelper
{
public static string GetDisplayName<T>(Expression<Func<T, object>> propertyExpression)
{
Expression expression;
if (propertyExpression.Body.NodeType == ExpressionType.Convert)
{
expression = ((UnaryExpression)propertyExpression.Body).Operand;
}
else
{
expression = propertyExpression.Body;
}
if (expression.NodeType != ExpressionType.MemberAccess)
{
throw new ArgumentException("Must be a property expression.", "propertyExpression");
}
var me = (MemberExpression)expression;
var member = me.Member;
var att = member.GetCustomAttributes(typeof(DisplayAttribute), false).OfType<DisplayAttribute>().FirstOrDefault();
if (att != null)
{
return att.Name;
}
else
{
// No attribute found, just use the actual name.
return member.Name;
}
}
public static string GetDisplayName<T>(this T target, Expression<Func<T, object>> propertyExpression)
{
return GetDisplayName<T>(propertyExpression);
}
}
And here's some sample usage. Note that you really don't even need an instance to get this metadata, but I have included an extension method that may be convenient.
public static void Main(string[] args)
{
Class1 class1 = new Class1();
Console.WriteLine(class1.GetDisplayName(c => c.Title));
Console.WriteLine(PropertyHelper.GetDisplayName<Class1>(c => c.Title));
}