从一个IList<string>
或多个IEnumerable<string>
?创建逗号分隔的字符串值列表的最简洁方法是什么?
String.Join(...)
上的操作string[]
,从而可能是麻烦的与当类型,如工作IList<string>
或IEnumerable<string>
不能容易地被转换成一个字符串数组.
更具体地说,当异常包含自定义对象时,这些自定义对象本身可以是自身可序列化的
举个例子:
public class MyException : Exception
{
private readonly string resourceName;
private readonly IList<string> validationErrors;
public MyException(string resourceName, IList<string> validationErrors)
{
this.resourceName = resourceName;
this.validationErrors = validationErrors;
}
public string ResourceName
{
get { return this.resourceName; }
}
public IList<string> ValidationErrors
{
get { return this.validationErrors; }
}
}
Run Code Online (Sandbox Code Playgroud)
如果此异常序列化和反序列化,则不会保留两个自定义属性(ResourceName
和ValidationErrors
).属性将返回null
.
是否有用于实现自定义异常序列化的通用代码模式?
简单的问题,但我敢打赌,在这里询问可能比试图理解文档更直接MessageFormat
:
long foo = 12345;
String s = MessageFormat.format("{0}", foo);
Run Code Online (Sandbox Code Playgroud)
观察值为"12,345".
期望值是"12345".
我正在尝试在批处理文件中使用以下验证逻辑,但即使没有为批处理文件提供参数,"使用"块也永远不会执行.
if ("%1"=="") goto usage
@echo This should not execute
@echo Done.
goto :eof
:usage
@echo Usage: %0 <EnvironmentName>
exit 1
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在Java 中使用Iterable<T>
vs. 的注意事项是什么Collection<T>
?
例如,考虑实现一个主要涉及包含Foo
s 集合和某些关联元数据的类型.此类型的构造函数允许一次性初始化对象列表.(可以稍后设置元数据.)此构造函数应接受哪种类型?Iterable<Foo>
,还是Collection<Foo>
?
这个决定有哪些考虑因素?
继通过模式库类型,如提出ArrayList
(可从任何被初始化Collection
,但不是一个Iterable
),会导致我使用Collection<Foo>
.
但是,为什么不接受Iterable<Foo>
,因为这足以满足初始化需求?为什么要求Collection
消费者提供更高级别的功能(),而不是严格必要的(Iterable
)?
我bool
从(非泛型,异构)集合中提取值.
该as
运营商只能与引用类型使用,所以这是不可能做到的使用as
尝试安全播到bool
:
// This does not work: "The as operator must be used with a reference type ('bool' is a value type)"
object rawValue = map.GetValue(key);
bool value = rawValue as bool;
Run Code Online (Sandbox Code Playgroud)
有没有类似的东西可以做到安全地将对象转换为值类型而没有InvalidCastException
if的可能性,无论出于何种原因,该值不是布尔值?
是否可以创建一个使用EasyMock实现多个接口的模拟对象?
例如,界面Foo
和界面Closeable
?
在Rhino Mocks中,您可以在创建模拟对象时提供多个接口,但EasyMock的createMock()
方法只需要一种类型.
难道possbile与EasyMock的实现这一点,而不诉诸创建扩展双方暂时接口的后退Foo
和Closeable
,然后嘲讽吗?
我想在IIS服务器上创建一个网站时收到错误.我使用的是Windows 7和Visual Studio 2010.
我是否必须为IIS注册或配置asp.net 4.0?
我想提供有关正在使用的JAXP实现以及从中加载的JAR文件的诊断信息.
实现此目的的一种方法是在例如a中创建DocumentBuilderFactory
,然后检查该类的属性:
private static String GetJaxpImplementation() {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Class<? extends DocumentBuilderFactory> c = documentBuilderFactory.getClass();
Package p = c.getPackage();
CodeSource source = c.getProtectionDomain().getCodeSource();
return MessageFormat.format(
"Using JAXP implementation ''{0}'' ({1}) version {2} ({3}){4}",
p.getName(),
p.getImplementationVendor(),
p.getSpecificationVersion(),
p.getImplementationVersion(),
source == null ? "." : " loaded from: " + source.getLocation());
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一点,也许无需创建DocumentBuilderFactory
?
我想有效地限制事件流,以便在收到第一个事件时调用我的委托,但如果收到后续事件则不会持续1秒.在超时(1秒)到期后,如果收到后续事件,我希望调用我的代理.
使用Reactive Extensions有一种简单的方法吗?
示例代码:
static void Main(string[] args)
{
Console.WriteLine("Running...");
var generator = Observable
.GenerateWithTime(1, x => x <= 100, x => x, x => TimeSpan.FromMilliseconds(1), x => x + 1)
.Timestamp();
var builder = new StringBuilder();
generator
.Sample(TimeSpan.FromSeconds(1))
.Finally(() => Console.WriteLine(builder.ToString()))
.Subscribe(feed =>
builder.AppendLine(string.Format("Observed {0:000}, generated at {1}, observed at {2}",
feed.Value,
feed.Timestamp.ToString("mm:ss.fff"),
DateTime.Now.ToString("mm:ss.fff"))));
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
当前输出:
Running...
Observed 064, generated at 41:43.602, observed at 41:43.602
Observed 100, generated at 41:44.165, observed at 41:44.602
Run Code Online (Sandbox Code Playgroud)
但我想观察(时间戳显然会改变)
Running...
Observed 001, generated …
Run Code Online (Sandbox Code Playgroud) java ×4
c# ×3
.net ×2
.net-4.0 ×1
arguments ×1
asp.net ×1
batch-file ×1
casting ×1
collections ×1
diagnostics ×1
easymock ×1
exception ×1
jaxp ×1
mocking ×1
rhino-mocks ×1
string ×1
unit-testing ×1
version ×1