我遇到了代码中的错误,只有在启用了优化的情况下构建代码时才会重现.我制作了一个控制台应用程序来复制测试逻辑(下面的代码).你会看到,当启用优化时,'value'在执行这个无效逻辑后变为null:
if ((value == null || value == new string[0]) == false)
Run Code Online (Sandbox Code Playgroud)
修复是直截了当的,并在违规代码下方注释掉.但是......我更担心的是我可能遇到了汇编程序中的错误,或者其他人可能会解释为什么将值设置为null.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace memory_testing
{
class Program
{
sta tic void Main(string[] args)
{
while(true)
{
Console.Write("Press any key to start...");
Console.ReadKey();
Console.WriteLine();
PrintManagerUser c = new PrintManagerUser();
c.MyProperty = new string[1];
}
}
}
public class PrintManager
{
public void Print(string key, object value)
{
Console.WriteLine("Key is: " + key);
Console.WriteLine("Value is: " + value);
}
}
public class PrintManagerUser …Run Code Online (Sandbox Code Playgroud) 我不确定是否应该使用这种模式但是情况如下:
我有许多实现接口的具体类:
public interface IPerformAction
{
bool ShouldPerformAction();
void PerformAction();
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类检查输入以确定是否应该执行ShouldPerformAction.问题是新检查会相当频繁地添加.检查类的接口定义如下:
public interface IShouldPerformActionChecker
{
bool CheckA(string a);
bool CheckB(string b);
bool CheckC(int c);
// etc...
}
Run Code Online (Sandbox Code Playgroud)
最后,我目前有具体的类使用特定于该具体类的数据调用每个检查器方法:
public class ConcreteClass : IPerformAction
{
public IShouldPerformActionCheck ShouldPerformActionChecker { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
public int Property3 { get; set; }
public bool ShouldPerformAction()
{
return
ShouldPerformActionChecker.CheckA(this.Property1) ||
ShouldPerformActionChecker.CheckB(this.Property2) ||
ShouldPerformActionChecker.CheckC(this.Property3);
}
public void PerformAction()
{
// do …Run Code Online (Sandbox Code Playgroud)