使用 FileHelpers 模块时如何避免警告 CS0649
How to avoid warning CS0649 while using FileHelpers Module
我正在使用 FileHelpers nuget 来读取文件。它正常工作,但当我尝试在 Visual Studio.
中调试时它会向我发出警告
如何摆脱warning CS0649: Field 'Orders.Freight' is never assigned to, and will always have its default value null
?
class Orders : INotifyRead
{
[FieldFixedLength(10)]
public string Freight;
public void BeforeRead(BeforeReadEventArgs e)
{
if (e.RecordLine.StartsWith("Machine"))
// ||
// e.RecordLine.StartsWith("-"))
e.SkipThisRecord = true;
}
public void AfterRead(AfterReadEventArgs e)
{
// we want to drop all records with no freight
if (Freight == "_raw")
e.SkipThisRecord = true;
}
}
你基本上有两个选择,走哪条路真的取决于意图(建议一个或另一个是主观的)。首先,如果 Orders
类型的设计要求规定它应该具有空默认值,则可以消除警告。
public string Freight = null;
以上仅阐明了该意图,因此消除了警告。
另一种方法是像其他答案提到的那样抑制警告。在您的情况下,如果假设应该通过反射设置该值,那么在这种情况下,即使不是更可取,这种替代方案似乎也是合理的。
不,不要明确地将默认值分配给 Freight
。
该警告是合理的,因为您从未真正为该字段赋值。
您没有分配值,因为该字段是通过魔法填充的。 (顺便说一下,这就是我不喜欢魔法的原因;但这完全是另一回事。)
因此,最好的方法是承认警告是合法的但已说明这一事实,并明确禁止警告。
所以,看看 #pragma warn
指令的文档:
https://msdn.microsoft.com/en-us/library/441722ys.aspx
为了完整起见,我将合并 blins 的回答和 Mike 的回答——没有原创内容,只是想帮助下一个浏览此页面的人。
Per blins:您可以将值设置为 null 和第一个警告 "Field XYZ is assigned to but never used"
public string Freight = null; //or = "", or = default(string) (which is null)
Per Mike,他所说的 "magic" 是反射。该变量在运行时分配给。这是编译器无法检测到的。有关 Mike 关于抑制此处警告的回答的更多信息:Suppressing "is never used" and "is never assigned to" warnings in C#
要抑制 "Field XYZ is never used" 的警告,您可以这样做:
#pragma warning disable 0169
... field declaration
#pragma warning restore 0169
要抑制 "Field XYZ is never assigned to, and will always have its default value XX" 的警告,您可以这样做:
#pragma warning disable 0649
... field declaration
#pragma warning restore 0649
我正在使用 FileHelpers nuget 来读取文件。它正常工作,但当我尝试在 Visual Studio.
中调试时它会向我发出警告如何摆脱warning CS0649: Field 'Orders.Freight' is never assigned to, and will always have its default value null
?
class Orders : INotifyRead
{
[FieldFixedLength(10)]
public string Freight;
public void BeforeRead(BeforeReadEventArgs e)
{
if (e.RecordLine.StartsWith("Machine"))
// ||
// e.RecordLine.StartsWith("-"))
e.SkipThisRecord = true;
}
public void AfterRead(AfterReadEventArgs e)
{
// we want to drop all records with no freight
if (Freight == "_raw")
e.SkipThisRecord = true;
}
}
你基本上有两个选择,走哪条路真的取决于意图(建议一个或另一个是主观的)。首先,如果 Orders
类型的设计要求规定它应该具有空默认值,则可以消除警告。
public string Freight = null;
以上仅阐明了该意图,因此消除了警告。
另一种方法是像其他答案提到的那样抑制警告。在您的情况下,如果假设应该通过反射设置该值,那么在这种情况下,即使不是更可取,这种替代方案似乎也是合理的。
不,不要明确地将默认值分配给 Freight
。
该警告是合理的,因为您从未真正为该字段赋值。
您没有分配值,因为该字段是通过魔法填充的。 (顺便说一下,这就是我不喜欢魔法的原因;但这完全是另一回事。)
因此,最好的方法是承认警告是合法的但已说明这一事实,并明确禁止警告。
所以,看看 #pragma warn
指令的文档:
https://msdn.microsoft.com/en-us/library/441722ys.aspx
为了完整起见,我将合并 blins 的回答和 Mike 的回答——没有原创内容,只是想帮助下一个浏览此页面的人。
Per blins:您可以将值设置为 null 和第一个警告 "Field XYZ is assigned to but never used"
public string Freight = null; //or = "", or = default(string) (which is null)
Per Mike,他所说的 "magic" 是反射。该变量在运行时分配给。这是编译器无法检测到的。有关 Mike 关于抑制此处警告的回答的更多信息:Suppressing "is never used" and "is never assigned to" warnings in C#
要抑制 "Field XYZ is never used" 的警告,您可以这样做:
#pragma warning disable 0169
... field declaration
#pragma warning restore 0169
要抑制 "Field XYZ is never assigned to, and will always have its default value XX" 的警告,您可以这样做:
#pragma warning disable 0649
... field declaration
#pragma warning restore 0649