Click here to Skip to main content
15,885,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends

Is that possible to change the value of method parameter by using custom attribute?

I want to Url decode string parameter by custom attribute and I don't want to go each and every method to write Url decode code for string value, I want to write one custom attribute to get method parameter value and decode it so I can assign attribute to all methods,

for example

C#
[MyCustomAttribute]
public void MyMethodName(string p1){
}


In MyCustomAttribute class, the parameter p1 will be decoded. If it is not possible then Is there any other way to accomplish my task instead of doing code in all methods.

Thanks
Imrankhan
Posted
Updated 21-Aug-13 5:16am
v2

1 solution

If I understand what you are asking, no.

All attributes do is attach meta data in the Common Intermediate Language (CIL) to a class, method or other block of code; it does not alter how the code is compiled to CIL. A part of this compiling is to "mangle" method names, adding the return type and the order and types of the parameters. For example,
C#
public void MyMethodName(string p1)

might get compiled to something like _MyMethodName__void_string, while
C#
public void MyMethodName(string p1, int p2)
public void MyMethodName(int p1, string p2)

might be compiled to _MyMethodName__void_int_string and _MyMethodName__void_string_int. This is how the Framework tells the difference between overloads and checks that parameters have the correct type. No attribute can change this behavior.

So if you want to change the parameter type, you cannot use attributes: they MUST be changed in the source code. If you want MyMethodName to take several different types of parameters, you must create a different method call for each parameter type. Or, you can have a single method with a parameter of type Object, but that would mean having to do your type checking manually.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900