1
votes

i want to create an attribute using postsharp that will HTMLIncode input parameters. Something like this:

public void Mymethod([HTMLEncode]string parm1,[HTMLEncode]string parm2){
    ....
}

I want to place it selectively on input parameters. Is this possible?

1

1 Answers

0
votes

The method's input parameters can be modified using MethodInterceptionAspect. In your particular case you can implement an IAspectProvider in your HTMLEncodeAttribute class and provide the interception aspect on the corresponding declaring method. For example:

[AttributeUsage(AttributeTargets.Parameter)]
public class HTMLEncodeAttribute : Attribute, IAspectProvider
{
    public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
    {
        ParameterInfo parameter = (ParameterInfo)targetElement;
        IAspectRepositoryService aspectRepositoryService = PostSharpEnvironment.CurrentProject.GetService<IAspectRepositoryService>();

        if (!aspectRepositoryService.HasAspect(parameter.Member, typeof(HTMLEncodeImplAspect)))
        {
            yield return new AspectInstance(parameter.Member, new HTMLEncodeImplAspect());
        }
    }
}

[PSerializable]
public class HTMLEncodeImplAspect : MethodInterceptionAspect
{
    private List<int> encodedParams;

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        this.encodedParams = new List<int>();
        ParameterInfo[] allParams = method.GetParameters();
        for (int i = 0; i < allParams.Length; i++)
        {
            if (allParams[i].GetCustomAttribute(typeof(HTMLEncodeAttribute)) != null)
            {
                this.encodedParams.Add(i);
            }
        }
    }

    public override void OnInvoke(MethodInterceptionArgs args)
    {
        foreach (int p in this.encodedParams)
        {
            args.Arguments.SetArgument(p, "encoded value");
        }
        args.Proceed();
    }
}