0

This may well not be possible, but if it is possible it will save me a great deal of time in a large project.

Let's say I have an extension method to format a string. The implementation of the extension method isn't important, but imagine it is this:

public static string AddPrefix(this string input) {
       return string.Format("{0}{1}", "pre_", input);
}

What I would love to be able to do it add a data annotation to a field in my model that I want the extension method to be applied to. I don't want to validate user input to match the format of the extension method, but I want to apply the extension method before submitting the data for any field it would apply to.

For instance, I might have:

public class Person {

    [Prefix]
    public string Forename {get;set;}
}

I would then want to apply the extension method to the forename before submitting the model to a database or webservice.

I'm imagining this isn't possible, so if not can anyone point in the direction of the best way to apply the extension method - there might be up to 50 fields spread across all sorts of classes in my domain model.

darth_phoenixx
  • 932
  • 12
  • 23

3 Answers3

1

I searched around a little and find this answer that says that you basicaly should not modify value of property from attribute. It just wrong. But here is some way.

public class PrefixAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
       //try to modify text
            try
            {
                validationContext
                .ObjectType
                .GetProperty(validationContext.MemberName)
                .SetValue(validationContext.ObjectInstance, string.Format("{0}{1}", "pre_", value.ToString()), null);
            }
            catch (System.Exception)
            {                                    
            }

        //return null to make sure this attribute never say iam invalid
        return null;
    }
}
Community
  • 1
  • 1
teo van kot
  • 12,350
  • 10
  • 38
  • 70
1

One option is to leverage Aspect Oriented Programming.

This would not require an extension method.

PostSharp is popular for .NET development.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • This definitely looks like it could be a potential solution. Never heard of this before so it looks like I've got some good reading material to keep me going now :) – darth_phoenixx Feb 26 '15 at 20:43
  • It injects code into your MISL before compiling. Great for logging, security, and any other cross-cutting concerns. – Scott Nimrod Feb 26 '15 at 22:24
0

It sounds to me that you want to create a custom data annotation, is that the case?

If so then this presents a possibility.

seguya
  • 56
  • 2
  • A custom data annotation would be perfect, but I don't want it to validate the data. Just a custom data annotation to signify that the value should have the extension method applied to it. – darth_phoenixx Feb 26 '15 at 20:29