我正在尝试克服WCF和枚举的问题,我正在尝试将对象从服务器传递到包含枚举的客户端(或其他服务器).枚举以1开头,故意.枚举初始化时以及在其中定义值时,一切都很顺利,但是当它未在枚举中定义时,我得到了这个非常好的(并且非常具有描述性(...))错误消息:
"底层连接已关闭:连接意外关闭."
我试图实现的是当我得到这个场景时,无论是从数据库中的损坏数据(无论如何都被转换为枚举,这是非常奇怪的),或者当开发人员忘记在启动对象时设置枚举值时,要获得有意义的消息,例如"枚举值无效,请键入:{0},值:{1}".
我试图在类的枚举和getter中使用"Enum.IsDefined",并将有意义的异常抛给客户端(或其他服务器),但仍然出现"连接关闭"错误(当允许调试服务器我得到了有意义的消息,但仅在服务器端).
这是enum setter和getter的片段:
private TestEnum m_TestEnum;
[DataMember]
public TestEnum TestEnum
{
get
{
if (Enum.IsDefined(typeof(TestEnum), m_TestEnum))
{
return m_TestEnum;
}
else
{
throw new ApplicationException("Enum value is not valid: " + m_TestEnum);
}
}
set
{
if (Enum.IsDefined(typeof(TestEnum), value))
{
m_TestEnum = value;
}
else
{
throw new ApplicationException("Enum value is not valid: " + value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
从0开始枚举(带有"未知"值)不够好,因为我仍然可以获得枚举中不存在的值.我可以结合两种解决方案,我检查"IsDefined"并将枚举设置为"未知"值,但仍然 - 这不是理想的解决方案,因为我们想知道这些情况,以便在开发周期中解决它们.
你说什么?谢谢,Nir.
Hello Functional C#Friends,
所以这次我试图压缩我的代码并编写更多功能,lambda样式,以及我想创建不必要的列表和类,让编译器为我做的工作.我确实设法以功能方式转换了一小段代码,但之后我不知道如何去做.
var errorList = new List<DataRow>();
IEnumerable<DataRow> resultRows = GetResultRows();
resultRows
.Filter(row => row.Field<string>("status").Equals("FAILURE", StringComparison.InvariantCultureIgnoreCase))
.ForEach(row => { errorList.Add(row); });
if (errorList.Count > 0)
{
var excludedBooks = new List<string>();
foreach (DataRow row in errorList)
{
if (ToUseBooksList.Contains((string)row["main_book"]))
{
BookCheckResults.AddRow(string.Format("Error for MainBook {0}, RiskType {1}",
row["main_book"], row["risk_type"]));
if (!excludedBooks.Contains((string)row["main_book"]))
{
excludedBooks.Add((string)row["main_book"]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的扩展方法:
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
if (collection == null)
throw new ArgumentNullException("collection");
if (action == null)
throw …
Run Code Online (Sandbox Code Playgroud) 花了一点时间想知道为什么我的应用程序在附加调试器的情况下非常缓慢地运行特定场景,我发现这是由于有一个条件断点(其条件永远不会被满足).这似乎是合理的,因为CPU会发出断点信号,VS需要在允许执行继续之前评估条件.这些转变必须是昂贵的.
我假设未执行的代码路径中的断点没有运行时影响.
所以我的问题是双重的:
当然,如果我上面提到的任何事情都没有意义,那么请指出我正确的方向.
.net debugging breakpoints conditional-breakpoint visual-studio
我有一个方法:
private String getProperty(String property) throws IOException,ConfigException {
// first test if the config file exists.
String propertyFile = "DataTransfer/Config/DataTransfer.properties";
if(!new File(propertyFile).exists()) {
throw new ConfigException("the config file doesn't exist." +
" Make sure the file : \""+propertyFile+"\" exists.");
}
// retrieve the property
Properties configFile = new Properties();
configFile.load(this.getClass().getClassLoader()
.getResourceAsStream(propertyFile));
String prop = configFile.getProperty(property);
return prop;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我一直在java.lang.NullPointerException
达到这个ConfigFile.load()
水平.
我在调试模式下检查了我的变量,它们都不是null.
我不知道这个例外的原因是什么.
我不知道如何给这个更好的标题,因为我不知道这个模式在Java中被称为什么.
现在我有一个带有此签名的方法:
public Directory getDirectory(Class<? extends Directory> type) { ... }
Run Code Online (Sandbox Code Playgroud)
你这样称呼它:
MyDirectory directory = (MyDirectory)getDirectory(MyDirectory.class);
Run Code Online (Sandbox Code Playgroud)
对类型的约束确保MyDirectory
必须从中派生Directory
.
我真正想要做的是避免强制转换并减少所需的代码量.在C#中你可以说:
MyDirectory directory = getDirectory<MyDirectory>();
Run Code Online (Sandbox Code Playgroud)
有没有办法在Java中做到这一点或类似的东西?我从1.4版开始就没有编写任何Java代码!
我正在使用WebClient.DownloadFileAsync
获取一批文件.但是有些文件不完整,没有例外.
我的问题是,如何在下载的文件未完成时进行标记?没有md5校验和来验证.
代码段是:
using (WebClient client = new WebClient())
{
Uri sUri = new Uri(sFileLink);
client.DownloadFileAsync(sUri, myPath);
}
Run Code Online (Sandbox Code Playgroud) 是否可以向IQueryable添加扩展方法,将其转换为另一种类型的IQueryable?
我在寻找像这样的东西;
IQueryable<foo> source;
IQueryable<bar> result = source.Convert<bar>();
Run Code Online (Sandbox Code Playgroud)
我有这个,显然它不起作用.大声笑
public static IQueryable<T1> Convert<T2>(this IQueryable<T1> source) where T1 : class
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我有一个std::shared_ptr<MotionTask>
对象矢量,我偶尔需要清理它.
// this assert passes
assert(std::all_of(d_tasks.begin(), d_tasks.end(),
[](shared_ptr<MotionTask> task) { return bool(task); }));
// Remove any committed tasks for which the corresponding module has completed
d_tasks.erase(
remove_if(
d_tasks.begin(),
d_tasks.end(),
[module](shared_ptr<MotionTask> const& task)
{
return task->isCommitted() && task->getModule() == module;
}
)
);
// this assert fails
assert(std::all_of(d_tasks.begin(), d_tasks.end(),
[](shared_ptr<MotionTask> task) { return bool(task); }));
Run Code Online (Sandbox Code Playgroud)
最后一个assert
是失败的,因为在任务的向量中,一个是null(false).
我不明白如何调用erase
会使成员无效.我无法在单元测试中重现这一点.
是否有可以从上面的代码中看到的解释,如果没有,我可以尝试调试这个?
想象一下一些文字:
<TextBlock>Loading...</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我想要一个简单的省略号(...
字符)动画,它在它之间振荡.
,..
并且...
在一个缓慢的循环中,以给人一种正在发生的事情的印象.
有没有一种简单的方法在XAML for WPF中执行此操作?