我真的希望能够使用某种类型的属性来装饰我的类,这将强制使用using语句,以便始终安全地处理类并避免内存泄漏.有人知道这种技术吗?
我只想验证一些事情.我相信如果我将using命令应用于SqlDataReader,它将关闭数据读取器并处理它.例如:
Using sdr As SqlDataReader = cm.ExecuteReader()
Dim someInt As Integer = sdr.GetInt32(0)
'other details and actions
End Using
Run Code Online (Sandbox Code Playgroud)
是否会在退出Using代码块后关闭sdr SqlDataReader .(我相信它会,但只是想验证.)
我知道C++/CLI相当于这个C#代码:
using (SomeClass x = new SomeClass(foo))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是:
{
SomeClass x(foo);
// ...
}
Run Code Online (Sandbox Code Playgroud)
但有没有类似简洁和类似RAII的方式来表达这一点:
using (SomeClass x = SomeFunctionThatReturnsThat(foo))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
要么:
SomeClass x = SomeFunctionThatReturnsThat(foo);
using (x)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
?我最接近的工作示例是:
SomeClass^ x = SomeFunctionThatReturnsThat(foo);
try
{
// ...
}
finally
{
if (x != nullptr) { delete x; }
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不太好.
我创建了一个解决方案Foo.添加了一个名为Foo.Common
Added a console app的类库,用于调用来自调用的库代码ConsoleApp.
我引用了Foo.CommonfromConsoleApp和typed:
using Foo.Common;
public class Program
{
CommonClass c = new CommonClass();
static void Main(string[] args)
{
}
}
Run Code Online (Sandbox Code Playgroud)
并得到这个:
Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1
我为什么要这个?
这是怎么回事?
我知道我们可以在一个使用块中创建多个相同类型的实例!但有没有办法可以在一个使用块中嵌套或写入不同类型的实例?
我刚看了这个,到目前为止它似乎是唯一的选择 http://blogs.msdn.com/b/ericgu/archive/2004/08/05/209267.aspx
我刚刚设置了一个TFS(2012)服务器,现在我正在尝试通过TFS Build服务器构建完整的代码(在VS 2010中用.NET 4.0编写).但在我的解决方案中,我还有一个包含链接文件的WCF RIA项目,因为它们也在其他地方使用,并且不可能在WCF/Silverlight中添加对常规.NET二进制文件的引用.
一切都在我的开发机器上构建没有任何问题但是当我全部检查时,创建一个标准的构建定义并运行该构建定义我遇到了以下问题.链接文件usings(例如UsingNamespace)也是由我们构建并在WCF/Silverlight之前构建的其他项目,但是在通过TFS构建服务器构建时弹出以下错误:
找不到类型或命名空间'UsingNamespace'(您是否缺少using指令或程序集引用?)'
我看过这个问题有什么解决办法吗?
编辑1
只是尝试设置Copy to Output Directory链接文件的属性,Copy Always但这仍然给我同样的错误,因为我期待.问题是链接文件放在可以使用的地方,usings但WCF RIA服务无法访问/查找使用.
编辑2
刚刚尝试了我的本地测试TFS,我可以在那里做我想做的事情,在那里我做了一个构建定义,只需要使用链接文件构建项目所需的解决方案.这没有任何问题.然后我在我们的TFS服务器上尝试了相同的新构建定义,它具有与我的测试TFS相同的解决方案,在这里它不起作用.我确定的唯一区别是我的测试TFS是TFS 2012 Update 1,而我的生产TFS还没有更新1.我下周试着安装它.
编辑3
我刚刚将生产TFS更新为Update 1,但它仍然无法使用我的临时构建定义,该定义仅包含使用链接文件构建silverlight应用程序所需的项目.两个工作区在两个服务器上都是相同的,要构建的项目也是相同的.
假设我有一个一次性对象MyDisposable,它将另一个一次性对象作为构造函数参数.
using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable()))
{
//whatever
}
Run Code Online (Sandbox Code Playgroud)
假设myDisposable不处理它AnotherDisposable内部的处理方法.
这只能正确处理myDisposable吗?还是处理它AnotherDisposable?
有人可以帮我理解下面的代码
public Font MyFont { get; set; }
void AssignFont()
{
using (Font f = new Font("Shyam",2))
{
this.MyFont = f;
}
}
Run Code Online (Sandbox Code Playgroud)
将处置对象分配给MyFont属性是否有效?
我的问题在这里,我正在学习JavaScript,但根本不是编程新手.我理解提升,但是使用严格模式不应该产生错误,并且当6被分配给未声明的变量或文件时被捕获.getElement ...被分配x这不会产生错误所以我的诊断就是提升仍在继续...我不喜欢并想要摆脱使用严格.使用Chrome版本42.0.2311.152 m作为我的浏览器
function strictMode(){
'use strict';
try {
x = 6;
document.getElementById('hoisting').innerHTML = x;
var x;
}
catch(err) {
document.getElementById('error_report').innerHTML =
"There was an error that occured (Were in Strict Mode)" +
" " + err.message;
}
}
Run Code Online (Sandbox Code Playgroud) 我经常看到人们using在他们的Haxe代码中使用关键字.它似乎追随了这些import陈述.
例如,我发现这是一个代码片段:
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
using Lambda;
Run Code Online (Sandbox Code Playgroud)
它做什么以及如何工作?