Search code examples
c#visual-studio-2012msbuildcode-generationbuild-process

Automatically alter C# WSDL generated code


In Visual Studio, is there a way to automatically alter source code and add a specific attribute to methods either prior to building or as part of the build process? Similar to code generation except I'm not generating the code, it already exists. Can something like T4 handle this? I don't want to manually add these attributes, I want it to be an automatic, repeatable process.

For example:

public class Test
{
    public void MethodOne()
    {
    }

    public void MethodTwo()
    {
    }
}

Automatically becomes:

public class Test
{
    [CustomAttribute]
    public void MethodOne()
    {
    }

    [CustomAttribute]
    public void MethodTwo()
    {
    }
}

Background:

I'm using a 3rd party product called Xamarin to build Android + iOS apps using .NET. I'm consuming a SOAP web service using WSDL.exe to automatically generate a proxy. I've added an a custom web service SOAP extension on the client side and the only reliable way I've found of wiring up that custom extension is adding a custom attribute to each web service method generated in the client web service proxy. I have to repeat this process each time I update the web service proxy since it generates fresh code each time.

Couple notes about Xamarin limitations. I don't have access to a web.config or app.config file, which is the normal way of wiring up custom web service SOAP extensions. Xamarin doesn't support web.config or app.config files. I can't use newer technologies like WCF because Xamarin doesn't fully support them (support is still in beta and I've encountered some bugs that prevented me from using them)


Solution

  • I accomplished this using your test class. I'm on Visual Studio 2010, but assume it'll work in 2012.

    Open up a Find/Replace window. Switch to the Quick Replace tab. Make sure Use Regular Expressions is checked under Find Options.

    Enter this for Find What:

    public void Method
    

    Enter this for Replace With:

    [CustomAttribute]\n\tpublic void Method