简体   繁体   English

自动生成的代码在Visual Studio 2015中引发很多警告

[英]auto-generated code throw lots of warning in visual studio 2015

I recently upgrade my visual studio from 2013 to 2015. now when I open my projects I see so many warning in auto generated code with code CA2235. 我最近将Visual Studio从2013年升级到2015年。现在,当我打开项目时,在自动生成的代码为CA2235的代码中看到了很多警告。

Here is one of the warnings: 这是警告之一:

Warning CA2235 Field extensionDataField is a member of type WebServiceInputTrafficFinesInquiry which is serializable but is of type System.Runtime.Serialization.ExtensionDataObject which is not serializable 警告CA2235字段extensionDataField是WebServiceInputTrafficFinesInquiry类型的成员,该成员可序列化,但类型为System.Runtime.Serialization.ExtensionDataObject,不可序列化

SimpayRobot C:\\Users\\Admin\\Desktop\\Projects\\SimpayRobot_SVN\\trunk\\SimpayRobot\\SimpayRobot\\Service References\\RahvarService\\Reference.cs 22 SimpayRobot C:\\ Users \\ Admin \\ Desktop \\ Projects \\ SimpayRobot_SVN \\ trunk \\ SimpayRobot \\ SimpayRobot \\ Service References \\ RahvarService \\ Reference.cs 22

All fields that cannot be serialized directly should have the NonSerializedAttribute. 所有不能直接序列化的字段都应具有NonSerializedAttribute。 Types that have the SerializableAttribute should not have fields of types that do not have the SerializableAttribute unless the fields are marked with the NonSerializedAttribute. 具有Seri​​alizableAttribute的类型不应具有不具有Seri​​alizableAttribute的类型的字段,除非这些字段用NonSerializedAttribute标记。

The project works just fine but it makes programming uncomfortable. 该项目工作正常,但使编程不舒服。 I don't know what to do. 我不知道该怎么办。 It is auto generated code and I don't want edit it. 它是自动生成的代码,我不想编辑它。

How can I get rid of these warnings? 我如何摆脱这些警告? One more thing: warnings are because of a soap service reference I added to the project. 还有一件事:警告是由于我向项目添加的肥皂服务参考。

UPDATE: I don't want to suppress the warning! 更新:我不想抑制警告! Why should I do it? 我为什么要这样做? I want solve the problem. 我想解决问题。

Here is line 22:(It is part of the auto generated code.) 这是第22行:(这是自动生成的代码的一部分。)

[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

I ran into the same problem when I started checking my source code with FxCops. 当我开始使用FxCops检查源代码时,我遇到了同样的问题。 Starting my investigation with this post it seems that the CA2235 warnings in reference.cs (automatically generated by WCF-services) are classified as an issue that will be solved in Visual Studio Update 3 (see this link https://github.com/dotnet/roslyn/issues/3898 for further information) 从这篇文章开始我的调查,似乎reference.cs(由WCF-services自动生成)中的CA2235警告被归类为将在Visual Studio Update 3中解决的问题(请参阅此链接https://github.com/ dotnet / roslyn / issues / 3898,以获取更多信息)

Regards JRB 关于JRB

Type of your field/property ExtensionDataObject is not serializable, but it's part of the class which is marked as serializable . 字段/属性的类型ExtensionDataObject不可序列化,但属于标记为serializable的类的一部分。

You should either mark your ExtensionDataObject as Serializable , or decorate with with NonSerializedAttribute . 您应该将ExtensionDataObject标记为Serializable ,或者用NonSerializedAttribute装饰。

For an example if you try to store your object in a session, it will not be able to serialize all of it's properties, and it could throw an exception. 例如,如果您尝试将对象存储在会话中,则它将无法序列化其所有属性,并且可能引发异常。

Have a deeper look at the warning. 进一步了解警告。 It tells you that a Field is (probably implicitly) marked as serializable, while its type is not serializable. 它告诉您,一个字段(可能是隐式)标记为可序列化的,而其类型则不可序列化。 It tells you to add [NonSerialized] to that filed. 它告诉您将[NonSerialized]添加到该文件中。

You can work this out like that (im just guessing what your code might look like): 您可以这样解决(我只是猜测您的代码可能是什么样子):

[Serializable]
public class WebServiceInputTrafficFinesInquiry 
{
    // ...

    [NonSerialized]
    ExtensionDataObject extensionDataField;

    // ...
}

See https://msdn.microsoft.com/en-us/library/ms182349.aspx for more information. 有关更多信息,请参见https://msdn.microsoft.com/zh-cn/library/ms182349.aspx

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

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