Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and modify a property value through a custom Attribute?

Tags:

c#

asp.net-mvc

I want to create a custom attribute that can be used on a property like:

[TrimInputString] public string FirstName { get; set; } 

that will be functional equivalent of

private string _firstName public string FirstName {   set {     _firstName = value.Trim();   }   get {     return _firstName;   } } 

So basically every time property is set the value will be trimmed.

How do I get the value parsed, modify that value and then set the property with the new value all from within the attribute?

[AttributeUsage(AttributeTargets.Property)] public class TrimInputAttribute : Attribute {    public TrimInputAttribute() {     //not sure how to get and modify the property here   }  } 
like image 538
David Glenn Avatar asked May 19 '10 09:05

David Glenn


People also ask

Which method is used to retrieve the values of the attributes?

First, declare an instance of the attribute you want to retrieve. Then, use the Attribute. GetCustomAttribute method to initialize the new attribute to the value of the attribute you want to retrieve. Once the new attribute is initialized, you simply use its properties to get the values.

How can you specify target for a custom attribute?

Which of the following are correct ways to specify the targets for a custom attribute? A. By applying AttributeUsage to the custom attribute's class definition.

What is the use of custom attributes?

A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.


2 Answers

iam doing this , not very convincing way but its working

demo class

public class User {  [TitleCase] public string FirstName { get; set; }  [TitleCase] public string LastName { get; set; }  [UpperCase] public string Salutation { get; set; }  [LowerCase] public string Email { get; set; }  } 

Writing Attribute for LowerCase, others can be written in the similar manner

public class LowerCaseAttribute : ValidationAttribute {     protected override ValidationResult IsValid(object value, ValidationContext validationContext)     {        //try to modify text             try             {                 validationContext                 .ObjectType                 .GetProperty(validationContext.MemberName)                 .SetValue(validationContext.ObjectInstance, value.ToString().ToLower(), null);             }             catch (System.Exception)             {                                                 }          //return null to make sure this attribute never say iam invalid         return null;     } } 

Not very elegant way as its actually implementing Validation attribute but it works

like image 86
Praveen Prasad Avatar answered Sep 17 '22 12:09

Praveen Prasad


That's not how attributes work. You can't access whatever the attribute is attached to from within the constructor.

If you want to make this work, you'll need to make some kind of processor class to which you pass the object, which then goes through the fields and does something depending on the attributes. The operation to do may be defined within the attribute (an abstract base attribute is handy here), but you'll still need to go through the fields by hand to apply the operation.

like image 27
Matti Virkkunen Avatar answered Sep 21 '22 12:09

Matti Virkkunen