如何在Web引用中处理CLS兼容?

时间:2021-12-24 01:58:32

I am turning on [assembly: System.CLSCompliant(true)] inside the assemblies of my C# solution.

我在我的C#解决方案的程序集中打开[assembly:System.CLSCompliant(true)]。

I am now getting a few warnings inside the generated code for a SharePoint Web Service.

我现在在生成的SharePoint Web服务代码中收到一些警告。

Here is one of the methods that are not CLS-compliant:

以下是不符合CLS的方法之一:

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/GetItem", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public uint GetItem(string Url, out FieldInformation[] Fields, [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] out byte[] Stream) {
        object[] results = this.Invoke("GetItem", new object[] {
                    Url});
        Fields = ((FieldInformation[])(results[1]));
        Stream = ((byte[])(results[2]));
        return ((uint)(results[0]));
    }

How can I remove this warning?

如何删除此警告?

Thank you, Keith

谢谢,基思

2 个解决方案

#1


I'd recommend you place your web references in a separate class library project that is not set to be CLS-compliant. Reference that library from your main code.

我建议您将Web引用放在一个单独的类库项目中,该项目未设置为符合CLS。从主代码中引用该库。

#2


You can mark the non-compliant methods with the [CLSCompliant(false)] attribute to avoid the warnings, but that seems to defeat the object of marking your assemblies as compliant in the first place: Presumably you want the code to actually be CLS-compliant.

您可以使用[CLSCompliant(false)]属性标记不合规方法以避免警告,但这似乎首先打败了将程序集标记为合规的对象:可能您希望代码实际上是CLS-兼容。

To make the code comply, you need to change the return type of the method from uint/UInt32. Exposed unsigned types aren't CLS-compliant.

要使代码符合要求,您需要从uint / UInt32更改方法的返回类型。暴露的无符号类型不符合CLS。

You could return long/Int64 instead. The long type is CLS-compliant and will safely handle any possible uint value.

您可以返回long / Int64。 long类型符合CLS,可以安全地处理任何可能的uint值。

If you can't, or won't, edit the generated code (to either add the attribute or alter the return type) then I think your only option is to move that code to a separate, non-compliant assembly as John suggests.

如果您不能或不会编辑生成的代码(添加属性或更改返回类型),那么我认为您唯一的选择是将该代码移动到John建议的单独的,不兼容的程序集中。

#1


I'd recommend you place your web references in a separate class library project that is not set to be CLS-compliant. Reference that library from your main code.

我建议您将Web引用放在一个单独的类库项目中,该项目未设置为符合CLS。从主代码中引用该库。

#2


You can mark the non-compliant methods with the [CLSCompliant(false)] attribute to avoid the warnings, but that seems to defeat the object of marking your assemblies as compliant in the first place: Presumably you want the code to actually be CLS-compliant.

您可以使用[CLSCompliant(false)]属性标记不合规方法以避免警告,但这似乎首先打败了将程序集标记为合规的对象:可能您希望代码实际上是CLS-兼容。

To make the code comply, you need to change the return type of the method from uint/UInt32. Exposed unsigned types aren't CLS-compliant.

要使代码符合要求,您需要从uint / UInt32更改方法的返回类型。暴露的无符号类型不符合CLS。

You could return long/Int64 instead. The long type is CLS-compliant and will safely handle any possible uint value.

您可以返回long / Int64。 long类型符合CLS,可以安全地处理任何可能的uint值。

If you can't, or won't, edit the generated code (to either add the attribute or alter the return type) then I think your only option is to move that code to a separate, non-compliant assembly as John suggests.

如果您不能或不会编辑生成的代码(添加属性或更改返回类型),那么我认为您唯一的选择是将该代码移动到John建议的单独的,不兼容的程序集中。