2

Is there a way to add an attribute to a method without modifying the class file?

E.g. I'm importing a WSDL which generates a Reference.cs containing a proxy class with methods.

I have written an attribute which does some work for me and adding it to the method like below, and all works fine:

Reference.cs file

public partial class Whatever
{
    [MyCustomAttrubute()]
    public void MyMethod(string bleh)
    {
        // do stuff
        return;
    }
}

However, my problem with this is that if the WSDL changes I will need to update it, which will automatically lose all my changes to Reference.cs. Can I add this attribute to the method from another file?

Community
  • 1
  • 1
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • In general no... See http://stackoverflow.com/q/3782405/613130 for properties... There is an hack through the `MetadataTypeAttribute` but it is only usable by some classes that know about it. – xanatos Jun 26 '15 at 11:04
  • I am not sure but it seems that your custom(modified a little) T4 template can help you. – Disappointed Jun 26 '15 at 11:12
  • If you like a world of pain you might try [dynamically generated code and compiling](https://msdn.microsoft.com/en-us/library/650ax5cx(v=vs.110).aspx). – Patrik Jun 26 '15 at 12:28

1 Answers1

-2

If your other method is also a string, can you not just concatenate the other method to this? In other words when you call MyMethod(string bleh), just pass 2 strings concatenated to it.

For example: MyMethod(string1 + "|" + string2);

this would pass 2 parameters concatenated with a pipe, then in the method you can use

string[] vals = val.Split('|');

which would give you an array of the parameters passed. this way you could also pass more than 2 parameters through.

then you could get the parameters as:

sting para1 = vals[0]; sting para2 = vals[1];

James Kirk
  • 24
  • 2