I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection
, MemoryStream
, etc.
我想知道是否存在某种备忘单,说明哪些对象符合using语句……SQLConnection,MemoryStream等等。
Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close()
before the closing using statement bracket.
更进一步,甚至可以展示其他的“拼图碎片”,比如如何实际调用connection.Close(),在使用语句括号结束之前。
Anything like that exist? If not, maybe we should make one.
诸如此类的存在吗?如果没有,也许我们应该做一个。
9 个解决方案
#1
10
Microsoft FxCop has a rule checking that you use an IDisposbale in a using block.
Microsoft FxCop有一个规则,用于检查您在一个use块中使用idispose sbale。
#2
14
Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/. Not sure if that's what you're looking for, but based on the original question, it sounds like it may be.
也许你可以浏览一下我在http://www.lancemay.com/2010/01/i处分—cheat-sheet/上的文章。不确定这是不是你要找的,但是根据最初的问题,听起来可能是。
#3
7
The following C# method will list all IDisposable types found in a certain assembly. (Used namespaces: System, System.Collections.Generic, System.IO, System.Reflection)
下面的c#方法将列出在某个程序集中找到的所有IDisposable类型。(使用名称空间:系统System.Collections。通用的,系统。IO,System.Reflection)
static void PrintDisposableTypesFromFile(String path)
{
Assembly assembly = Assembly.LoadFrom(path);
foreach (Type type in assembly.GetTypes())
if (type.GetInterface("IDisposable") != null)
Console.WriteLine(type.FullName);
}
The following C# method makes use of the previous one to do the same for all assemblies in a directory and its subdirectories, e.g. "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727":
下面的c#方法利用前面的方法对目录及其子目录中的所有程序集执行相同的操作,例如。“C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727”:
static void PrintDisposableTypesFromDirectory(DirectoryInfo dir, bool warn)
{
foreach (FileInfo file in dir.GetFiles("*.dll"))
{
try
{
PrintDisposableTypesFromFile(file.FullName);
}
catch (Exception ex)
{
if (warn)
{
Console.Error.WriteLine(
String.Format(
"WARNING: Skipped {0}: {1}",
new object[] { file.FullName, ex.Message }));
}
}
}
// recurse
foreach (DirectoryInfo subdir in dir.GetDirectories())
PrintDisposableTypesFromDirectory(subdir, warn);
}
I'm not sure the list of all disposables is very useful, but I've used similar code to find other interesting things like the full list of text encodings supported by the .NET framework.
我不确定所有可配置文件的列表是否非常有用,但我使用了类似的代码来查找其他有趣的东西,比如. net框架支持的文本编码的完整列表。
#4
4
If you are unsure whether a class implements IDisposable
or not, enclose it in a using
block regardless. If you get a compile error, just remove it. You'll only lose a few seconds typing time.
如果您不确定一个类是否实现了一次性使用,那么无论如何将它封装在一个使用块中。如果你得到一个编译错误,就删除它。你只会损失几秒钟的打字时间。
#5
3
In addition to the other answers, note that a class might implement IDisposable but not have Dispose come up in the intellisense list.
除了其他答案,请注意类可能实现IDisposable,但在intellisense列表中没有出现Dispose。
class MyClass :IDisposable
{
void IDisposable.Dispose()
{
/* Stuff */
}
}
#6
2
An simple way to get a list of types that implement IDisposable is to crack open Reflector, navigate to System.IDisposable
, expand the node, and then expand the 'Derived Types' node.
一种简单的方法来获取实现IDisposable的类型的列表,即打开反射器,导航到系统。i可支配,展开节点,然后展开“派生类型”节点。
To be sure that your list is complete, verify that all the assemblies you're using have been 'Open'ed in Reflector.
要确保您的列表是完整的,请验证您所使用的所有程序集都已在Reflector中“打开”。
#7
2
If you are using Visual Studio you can press F12 on a type declaration it will take you to a meta-data screen or the class definition (if you have the source code). If you keybindings are different right-click and "go to definition". From there you can see what a class implements. I suggest doing this for all classes the first time you encounter them to get a "feel" for what the class can do.
如果您正在使用Visual Studio,您可以按下F12的类型声明,它将带您到元数据屏幕或类定义(如果您有源代码)。如果您的键绑定是不同的,右键单击和“go to definition”。从那里您可以看到一个类实现了什么。我建议在第一次遇到他们时,为所有的类做这个,以获得类的“感觉”。
#8
1
With ReSharper you can show all derived types. Maybe you can do it with the object browser without ReSharper, too. Go to the interface definition and look for "show inheritors".
使用ReSharper,你可以显示所有派生类型。也许你也可以用对象浏览器来做,而不用ReSharper。转到接口定义并查找“显示继承器”。
#9
0
A quick&dirty way of checking if a type implements IDisposable is to create an instance of it and check if the instance has a Dispose() member function. If it does then you can be 99% sure that it implements IDisposable.
检查类型是否实现了IDisposable的一种快速方法是创建它的实例并检查实例是否具有Dispose()成员函数。如果它实现了,那么你可以99%确定它实现了IDisposable。
#1
10
Microsoft FxCop has a rule checking that you use an IDisposbale in a using block.
Microsoft FxCop有一个规则,用于检查您在一个use块中使用idispose sbale。
#2
14
Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/. Not sure if that's what you're looking for, but based on the original question, it sounds like it may be.
也许你可以浏览一下我在http://www.lancemay.com/2010/01/i处分—cheat-sheet/上的文章。不确定这是不是你要找的,但是根据最初的问题,听起来可能是。
#3
7
The following C# method will list all IDisposable types found in a certain assembly. (Used namespaces: System, System.Collections.Generic, System.IO, System.Reflection)
下面的c#方法将列出在某个程序集中找到的所有IDisposable类型。(使用名称空间:系统System.Collections。通用的,系统。IO,System.Reflection)
static void PrintDisposableTypesFromFile(String path)
{
Assembly assembly = Assembly.LoadFrom(path);
foreach (Type type in assembly.GetTypes())
if (type.GetInterface("IDisposable") != null)
Console.WriteLine(type.FullName);
}
The following C# method makes use of the previous one to do the same for all assemblies in a directory and its subdirectories, e.g. "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727":
下面的c#方法利用前面的方法对目录及其子目录中的所有程序集执行相同的操作,例如。“C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727”:
static void PrintDisposableTypesFromDirectory(DirectoryInfo dir, bool warn)
{
foreach (FileInfo file in dir.GetFiles("*.dll"))
{
try
{
PrintDisposableTypesFromFile(file.FullName);
}
catch (Exception ex)
{
if (warn)
{
Console.Error.WriteLine(
String.Format(
"WARNING: Skipped {0}: {1}",
new object[] { file.FullName, ex.Message }));
}
}
}
// recurse
foreach (DirectoryInfo subdir in dir.GetDirectories())
PrintDisposableTypesFromDirectory(subdir, warn);
}
I'm not sure the list of all disposables is very useful, but I've used similar code to find other interesting things like the full list of text encodings supported by the .NET framework.
我不确定所有可配置文件的列表是否非常有用,但我使用了类似的代码来查找其他有趣的东西,比如. net框架支持的文本编码的完整列表。
#4
4
If you are unsure whether a class implements IDisposable
or not, enclose it in a using
block regardless. If you get a compile error, just remove it. You'll only lose a few seconds typing time.
如果您不确定一个类是否实现了一次性使用,那么无论如何将它封装在一个使用块中。如果你得到一个编译错误,就删除它。你只会损失几秒钟的打字时间。
#5
3
In addition to the other answers, note that a class might implement IDisposable but not have Dispose come up in the intellisense list.
除了其他答案,请注意类可能实现IDisposable,但在intellisense列表中没有出现Dispose。
class MyClass :IDisposable
{
void IDisposable.Dispose()
{
/* Stuff */
}
}
#6
2
An simple way to get a list of types that implement IDisposable is to crack open Reflector, navigate to System.IDisposable
, expand the node, and then expand the 'Derived Types' node.
一种简单的方法来获取实现IDisposable的类型的列表,即打开反射器,导航到系统。i可支配,展开节点,然后展开“派生类型”节点。
To be sure that your list is complete, verify that all the assemblies you're using have been 'Open'ed in Reflector.
要确保您的列表是完整的,请验证您所使用的所有程序集都已在Reflector中“打开”。
#7
2
If you are using Visual Studio you can press F12 on a type declaration it will take you to a meta-data screen or the class definition (if you have the source code). If you keybindings are different right-click and "go to definition". From there you can see what a class implements. I suggest doing this for all classes the first time you encounter them to get a "feel" for what the class can do.
如果您正在使用Visual Studio,您可以按下F12的类型声明,它将带您到元数据屏幕或类定义(如果您有源代码)。如果您的键绑定是不同的,右键单击和“go to definition”。从那里您可以看到一个类实现了什么。我建议在第一次遇到他们时,为所有的类做这个,以获得类的“感觉”。
#8
1
With ReSharper you can show all derived types. Maybe you can do it with the object browser without ReSharper, too. Go to the interface definition and look for "show inheritors".
使用ReSharper,你可以显示所有派生类型。也许你也可以用对象浏览器来做,而不用ReSharper。转到接口定义并查找“显示继承器”。
#9
0
A quick&dirty way of checking if a type implements IDisposable is to create an instance of it and check if the instance has a Dispose() member function. If it does then you can be 99% sure that it implements IDisposable.
检查类型是否实现了IDisposable的一种快速方法是创建它的实例并检查实例是否具有Dispose()成员函数。如果它实现了,那么你可以99%确定它实现了IDisposable。