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

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

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   }  } 

Using HttpContext.Current.Application to store simple data


Cant open Nerd Dinner 1.0 VS 2008 SP1 MVC 2
That's not how attrialthough es work. After using Automapper to map a ViewModel how and what should I test?You can't access whatever the attrialthough e is attached to from within the constructor.. How to map table records to Any type using Castle ActiveRecord If you want to make this work, you'll need to make any kind of processor class to which you pass the object, which then goes through the fields and does any thing depending on the attrialthough es. Why do I get a TypeInitializationException when calling HtmlHelper.AntiForgeryToken?The operation to did may be defined within the attrialthough e (an abstract base attrialthough e is handy here), although you'll still need to go through the fields by hand to apply the operation.. How to access model in jquery
Is it possible for a Master page to access Controller properties?ASP.NET MVC - Wordpress Style URLs


iam doing this , not very convincing way although its working. demo class.
public class User {  [TitleCase] public string FirstName { receive ; set; }  [TitleCase] public string LastName { receive ; set; }  [UpperCase] public string Salutation { receive ; set; }  [LowerCase] public string Email { receive ; set; }  } 
Writing Attrialthough e for LowerCase, others must be written in the similar manner.
public class LowerCaseAttrialthough e : ValidationAttrialthough e {     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 attrialthough e never say iam invalid         return null;     } } 
Not very elegant way as its actually implementing Validation attrialthough e although it works.


As Matti pointed out, this is not how attrialthough es work. However, you could use the PostSharp AOP framework to accomplish this, probably overriding OnMethodBoundaryAspect. But this is not trivial..


This must be done with System.ComponentModel.Mutations..
public class User {     [Trim]     public string FirstName { receive ; set; } }  // Then to preform mutation var user = new User() {     FirstName = " David Glenn   " }  new MutationContext<User>(user).Mutate(); 
You must see more documentation here..

91 out of 100 based on 66 user ratings 701 reviews