Property of a custom .NET property?

EDIT: I would rather rephrase: How can I transfer the GET implementation of the Class property to / using a custom attribute? (I added the object creation attributes (classname, propertyname) to the attribute, however, I would prefer that these are automatically taken out of circulation.)

Public Class CustomClass <CustomAttributeClass(ClassName:="CustomClass", PropertyName = "SomeProperty")> _ Public Property SomeProperty() as String Get() as String //This implementation should be handled by the attribute class End Get Set(Byval value as String) Me._someProperty = value End Set End Property End Class 

Old question:

I want to create a custom property attribute for classes. I can create a class derived from an attribute and "tag" a property with an attribute, but where to go from here?

I have a repository where I can quickly get data based on attribute values. I would like to generalize the behavior of a property in an attribute, but I do not know how to go from here ... Any help would be greatly appreciated!

 Public Class CustomDataAttribute : Inherits Attribute Private _name As String Public Sub New(ByVal name As String) Me.Name = name End Sub Property Name() As String Get Return _name End Get Set(ByVal value As String) Me._name = value End Set End Property End Class Public Class CustomClass <CustomDataAttribute(Name:="CustomField")> _ Public Property CustomField() End Property End Class 
+4
source share
3 answers

You will need to use Reflection to find the attribute. In your case, you will get it from PropertyInfo.GetCustomAttributes ().

A more difficult part of using attributes is to find a suitable execution model for their actual use. Something like a compiler, designer, or class that serializes objects is obvious. Ease of use attributes quickly spiral down from there. This is almost always the wrong choice when you are trying to use an attribute where a virtual property is really needed. Getting attribute values ​​is very expensive, many orders of magnitude more expensive than getting a property value. Use them only when the reflection code works in human time (for example, compilers) or when the cost is negligible compared to the gain or overhead (common for any type of I / O).

+2
source

From your comments on the previous answer, I think you will find that .NET attributes are not as flexible as you want them to be.

You asked: "Is there any basic attribute property that has some events, such as onGet and onSet?" - not; attributes have no built-in interaction with their goals, whether it be methods or classes. In fact, at runtime you cannot even indicate what the purpose of the attribute is; you must first know the target (class, method, property, etc.), and then ask what attributes adorn it.

Secondly, attributes are not actually created until you request them. When you call GetCustomAttributes , the GetCustomAttributes system evaluates the assembly metadata and instantiates the attributes that were specified. If you call it twice in a row, you will get two sets of identical attributes.

Returning to your other question: if you want to know when a property decorated with your attributes is set or received, you need to implement INotifyPropertyChanged for all your related classes, write code to search all your classes for the properties marked with this attribute when loading the assembly, and then create some interactivity that hooked PropertyChanged events to any code you need to fire. (And this only notifies you of set operations, not get .)

I don't know if this is useful, but there you go. :-)

+2
source

Here is a class that helps handle custom attributes when using reflection. Pass the type as the contructor parameter.

 public class AttributeList : List<Attribute> { /// <summary> /// Gets a list of custom attributes /// </summary> /// <param name="propertyInfo"></param> /// <returns></returns> public static AttributeList GetCustomAttributeList(ICustomAttributeProvider propertyInfo) { var result = new AttributeList(); result.AddRange(propertyInfo.GetCustomAttributes(false).Cast<Attribute>()); return result; } /// <summary> /// Finds attribute in collection by its own type or parents type /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T FindAttribute<T>() where T : Attribute { return (T)Find(x => typeof(T).IsAssignableFrom(x.GetType())); } /// <summary> /// Finds attribute in collection by its own type or parents type /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List<T> FindAllAttributes<T>() where T : Attribute { return new List<T>(FindAll(x => typeof(T).IsAssignableFrom(x.GetType())).Cast<T>()); } /// <summary> /// Finds attribute in collection by its own type or parents type /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List<T> FindAllAttributes<T>(Type implementsType) where T : Attribute { return new List<T>(FindAll(x => implementsType.IsAssignableFrom(x.GetType())).Cast<T>()); } public bool IsAttributeSet<T>() where T : Attribute { return FindAttribute<T>() != null; } public TValue GetValueFromAttributeOrDefault<TAttr, TValue>(Func<TAttr, TValue> func, TValue defaultValue) where TAttr : Attribute { var attribute = FindAttribute<TAttr>(); return attribute == null ? defaultValue : func(attribute); } } 

0
source

Source: https://habr.com/ru/post/1303345/


All Articles