简体   繁体   English

自动更改C#WSDL生成的代码

[英]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? 在Visual Studio中,是否有一种方法可以在构建之前或在构建过程中自动更改源代码并将特定属性添加到方法中? Similar to code generation except I'm not generating the code, it already exists. 与代码生成类似,除了我不生成代码外,它已经存在。 Can something like T4 handle this? T4之类的东西可以处理吗? 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. 我正在使用名为Xamarin的第三方产品来使用.NET构建Android + iOS应用。 I'm consuming a SOAP web service using WSDL.exe to automatically generate a proxy. 我正在使用WSDL.exe自动生成代理的SOAP Web服务。 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. 我已经在客户端添加了一个自定义Web服务SOAP扩展,并且发现连接该自定义扩展的唯一可靠方法是向客户端Web服务代理中生成的每个Web服务方法添加一个自定义属性。 I have to repeat this process each time I update the web service proxy since it generates fresh code each time. 每次更新Web服务代理时,都必须重复此过程,因为它每次都会生成新代码。

Couple notes about Xamarin limitations. 关于Xamarin限制的几个注释。 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. 我无权访问web.config或app.config文件,这是连接自定义Web服务SOAP扩展的常规方法。 Xamarin doesn't support web.config or app.config files. Xamarin不支持web.config或app.config文件。 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) 我不能使用WCF等较新的技术,因为Xamarin不完全支持它们(该支持仍处于beta中,并且我遇到了一些错误使我无法使用它们)

I accomplished this using your test class. 我使用您的测试课程完成了此任务。 I'm on Visual Studio 2010, but assume it'll work in 2012. 我使用的是Visual Studio 2010,但假定它将在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM