在返回的方法中IEnumerable<>,我打开并循环遍历资源(例如,数据库行读取器).循环结束后,资源再次关闭.
但是,可能会发生呼叫者决定不完成枚举的情况.这使资源保持开放.
例:
IEnumerable<Foo> Bar ()
{
using (var r = OpenResource()) {
while (r.Read ()) {
yield return r;
}
}
}
// OK - this closes the resource again
foreach (var foo in Bar()) {
Console.WriteLine (foo);
}
// Not OK - resource stays open!
Console.WriteLine (Bar().First());
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我可以轻松取消枚举,即告诉它跳过循环的其余部分,或者将其丢弃(将清理代码放入Dispose)?
我考虑过返回一个Func<Result, bool>如此用户可以让它返回,false如果他完成迭代.同样,也可以使用某种取消令牌.但这两种方法对我来说都很麻烦.
我的类需要附加信息才能正确输出其状态,因此我添加了一个自定义 PrintSelf 方法,该方法采用适当的参数。
但是,恐怕我的大项目中仍然存在对ToString的调用,没有被新方法取代。如何找到对 ToString 的不正确调用?
我正在使用 VS 2015,但它似乎没有这种能力。
在 ToString 中抛出异常是一种显而易见的方式,但我不想这样做有两个原因:
ToString 仍然可以执行不同的工作并输出一些不依赖于添加的参数的东西。
没有办法获得完整的代码覆盖率,这意味着它只会找到一些隐式调用的实例,而不是(可靠地)所有这些实例。
在给定的字符串中,很容易搜索第一次出现的子字符串,如下所示:
int position = "01234".IndexOf ("23"); // returns 2
Run Code Online (Sandbox Code Playgroud)
我想搜索多个可能的字符串中的任何一个的第一次出现:
var options = new [] {"77", "34", "12"};
int position = "01234".ImaginaryIndexOf (options); // should return 1
Run Code Online (Sandbox Code Playgroud)
这样的函数似乎不存在于.NET框架中.我错过了吗?
编辑:为了澄清,我正在寻找一种适用于大输入和不均匀分布选项的方法.想象一下类似的东西
var options = new [] {"x", "y"};
new String ('x', 1000*1000).IndexOf (options)
Run Code Online (Sandbox Code Playgroud) 我正在实现一个C编译器,并发现了一个奇怪的问题.由于&优先级高于&&,所以将其视为二进制并且第一个操作数与第二个操作数相比似乎是合理的:
x && y = (x) & ( &(y) )
Run Code Online (Sandbox Code Playgroud)
C规范的语法概述似乎允许这种解释.我可能错过或误读了什么?
我对语法的理解:
andExpression:= equalityExpression | (andExpression'&'equalityExpression)| ...
...
unaryExpression:= postfixExpression | (('&'|'*'|'+'|' - '|'〜'|'!')castExpression)| ...
在.NET中,我想安排大量的Tasks,例如通过Task.Run(...). 有些任务的重要性较低,如果有较高优先级的任务可供执行,则调度程序应延迟这些任务。
有没有办法做到这一点?.NET 中似乎没有TaskScheduler支持任何优先级的调度。
这些任务是短期运行且无层级的。需要明确的是,这与执行任务的线程的优先级完全无关。
QueuedTaskScheduler来自ParallelExtensionsExtras似乎是我正在寻找的东西,但它已经七年没有维护了,缺乏文档,并且大多数与其相关的链接都已损坏 - 我宁愿不添加对它的依赖。
我有这样的代码:
public abstract class Base
{
// is going to be used in deriving classes
// let's assume foo is threadsafe
protected static readonly Foo StaticFoo = new Foo();
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2008的代码分析弹出以下消息:
CA2104 : Microsoft.Security : Remove the read-only designation from 'Base.StaticFoo' or change the field to one that is an immutable reference type. If the reference type 'Foo' is, in fact, immutable, exclude this message.
我的设计是否存在本质上的缺陷,或者我可以[SuppressMessage]在源代码中添加一个?
我通常m_在private田野和s_前面的static成员面前添加一个.
用像这样的代码
protected static readonly Random s_Random = new Random ();
Run Code Online (Sandbox Code Playgroud)
我通过VS2008的代码分析得到以下警告:
CA1709:Microsoft.Naming:通过将其更改为"S"来更正成员名称"Bar.s_Random"中's'的大小写.CA1707:Microsoft.Naming:从成员名称"Bar.s_Random"中删除下划线.如何解决这个问题?我应该简单地删除s_?或者为此警告添加全局抑制?
编辑:我的公司缺乏编码标准,所以我可以为我的代码定义它们.(是的我懂...)
如果你认为s_一般应该删除,我很高兴你能提供官方消息来源.
c# code-analysis fxcop naming-conventions visual-studio-2008
是否可以通过远程传输回传?我想做点什么myRemoteObject.PerformStuff( x => Console.WriteLine(x) );
如果没有,我将如何实现等效功能?
编辑:我知道Remoting被WCF取代了,但我仍然对这个具体案例感兴趣.(出于好奇,WCF中的问题怎么样?)
对于某些测试代码,我希望能够仅在几行中托管WCF服务.我想我会写一个简单的托管类:
public class WcfHost<Implementation, Contract> : IDisposable
where Implementation : class
where Contract : class
{
public readonly string Address = "net.tcp://localhost:8000/";
private ServiceHost _Host;
public WcfHost ()
{
_Host = new ServiceHost (typeof (Implementation));
var binding = new NetTcpBinding ();
var address = new Uri (Address);
_Host.AddServiceEndpoint (
typeof (Contract),
binding,
address);
_Host.Open ();
}
public void Dispose ()
{
((IDisposable) _Host).Dispose ();
}
}
Run Code Online (Sandbox Code Playgroud)
这可以像这样使用:
using (var host = new WcfHost<ImplementationClass, ContractClass> ()) {
Run Code Online (Sandbox Code Playgroud)
这种方法有什么问题吗?代码中是否存在缺陷(尤其是关于处置)?
我正在寻找使用继承的坏例子.我不是很有创意,所以这是我能想到的最好的:
class Car : public Engine {}
Run Code Online (Sandbox Code Playgroud)
汽车有发动机,但它不是发动机.
这可能有助于解释这个概念,但我相信还有更多的说明性例子?
c# ×7
.net ×4
c ×1
callback ×1
fxcop ×1
ienumerable ×1
inheritance ×1
parsing ×1
remoting ×1
servicehost ×1
string ×1
tostring ×1
wcf ×1