如何在 Web 参考中处理 CLS 兼容问题?
我正在 C# 解决方案的程序集中打开 [程序集:System.CLSCompliant(true)]。
我现在在为 SharePoint Web 服务生成的代码中收到一些警告。
以下是不符合 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]));
}
如何删除此警告?
谢谢你, 基思
I am turning on [assembly: System.CLSCompliant(true)] inside the assemblies of my C# solution.
I am now getting a few warnings inside the generated code for a SharePoint Web Service.
Here is one of the methods that are not CLS-compliant:
/// <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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
[CLSCompliant(false)]
属性标记不兼容的方法以避免警告,但这似乎首先破坏了将程序集标记为兼容的目标:大概您想要代码实际上符合 CLS。为了使代码符合要求,您需要从 < 更改方法的返回类型代码>uint/
UInt32
。 公开的无符号类型不符合 CLS。您可以返回
long
/Int64
代替。long
类型符合 CLS,并将安全地处理任何可能的uint
值。如果您不能或不会编辑生成的代码(以添加属性或更改返回类型),那么我认为您唯一的选择就是将该代码移动到单独的、不兼容的程序集 正如约翰建议的。
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.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.You could return
long
/Int64
instead. Thelong
type is CLS-compliant and will safely handle any possibleuint
value.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.
我建议您将 Web 引用放置在未设置为符合 CLS 的单独的类库项目中。 从主代码中引用该库。
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.