与使用块战斗,寻找一种安全的方式 [英] fighting with using block, looking for a safe way

查看:62
本文介绍了与使用块战斗,寻找一种安全的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家

请记住;我是c#的初学者。



a。)如果我设计了一个实现IDisposable的类:如果有人使用不在的类,是否有可能强制警告或更好的编译器错误一个使用块?



b。)我如何轻松/安全地确定一个类是否应该/只需要使用一个使用块?我是否真的要检查此类是否实现了IDisposable?



提前谢谢

Hello experts
Keep in mind; I’m beginner in c#.

a.) If I design a class which implements IDisposable: Is there a possibility to force a warning or much better a compiler error if someone does use the class not in a using block?

b.) How can I easy/safety determine, whether a class should/needs to be used only with a using block? Do I really have to check always whether this class implements IDisposable?

Thank you in advance

推荐答案

使用块意味着

A a =新A();

//使用

a.Dispose();



等于:



使用(A a =新A())

{

//使用

}



如果你的班级使用资源,那么实施IDisposable只是一个建议。

如果你的班级没有实施IDisposable,你只有你自己的Dispose()方法,你不能使用块。

同时,如果你正确实现IDisposable,使用bloacks(或a.Dispose)也是一个建议,因为GC将如果你不这样做,请帮助召唤处理。但你最好不要。:)
using blocks means
A a = new A();
//use a
a.Dispose();

above equal:

using(A a = new A())
{
//use a
}

Implementing IDisposable is just a advice,if your class uses resource.
if your class does not implement IDisposable,you just have your custom Dispose() method,you cant use using blocks.
meanwhile,if you implement IDisposable correctly,using bloacks(or a.Dispose) is a advice too,because GC will help to call dispose if you dont. but you'd better not.:)


(在比尔的建议下)



一个using语句是这样的:



(At Bill's suggestion)

A using statement works like this:

using (A a = new A())
{
    //Do whatever you want here
}

//Equivalent to:

{  //These are scoping, outside of it, a does not exist
    A a = new A();
    try
    {
        //Do whatever you want here
    }
    finally
    {
        a.Dispose();
    }
}





所以不仅要使用 Dispose 终结器,它保证如果在使用中有异常,它将被调用。这就是为什么总是建议使用使用语句而不是单独调用Dispose(),这样可以确保正确处理对象。



就原来的问题而言,格里夫钉了它,但这是我的两分钱:



A )不要试图让开发人员在代码周围使用某个构造,否则它将无法正常工作。虽然这似乎是它在你脑海中工作的唯一方式,但是有些用例不适合你的构造。为用户提供能力以正确使用您的代码,这意味着拥有正确的界面文档。



B)最简单的方法是在销毁对象时(将其设置为null),检查它是否有Dispose。如果是,请先调用Dispose。随着编程越来越好,将更容易分辨哪些类应该实现它,哪些不应该。这里的提示是,如果有内部需要拆除的东西,它将有一个Dispose(非托管资源,连接对象等)。





由TnTinMan提供:

MSDN文章:避免使用声明的问题 [ ^ ]讨论了using语句的一些可能问题,因为它在try ... finally块中的实际结构如何。



So not only does using call the Dispose finalizer, it guarantees that it will be called if there is an exception inside the using. That is why it's always recommended to use using statements over just calling Dispose() by itself, it makes sure that the object is properly disposed.

As far as the original question, Griff nailed it, but here's my two cents:

A) Do not attempt to pigeon hole a developer to use a certain construct around your code or it won't work properly. While this seems like it would be the only way it works in your mind, there are use cases out there that won't fit your construct. Give the user the ability to use your code correctly, which means having the right interface and the documentation to go with it.

B) The easiest way to tell is when you are destroying the object (setting it to null), check to see if it has a Dispose. If it does, call Dispose first. As you get better with programming it will be easier to tell which classes should implement it, and which shouldn't. The hint here is that if there is something that needs to be tore down internally, it will have a Dispose (unmanaged resources, connection objects, etc).


Courtesy of TnTinMan:
MSDN Article: Avoiding Problems with the Using Statement[^] which discusses some possible issues with the using statement due to how its actually structured inside a try...finally block.


a)否。

b)是。



更长的版本:

有无法强迫某人处理你的对象实例,因为在调用Dispose之前系统不知道他们已完成它 - 直接,通过使用块,或垃圾收集器。请记住,你不必使用使用块:

a) No.
b) Yes.

The longer version:
There is no way to force someone to Dispose of your object instance, because the system doesn't know they are finished with it until Dispose is called - either directly, via a using block, or by the Garbage Collector. Remember, you don;t have to use a using block:
MyClass mc = new MyClass();
...
mc.Dispose();

会做同样的事情,但可能会分散在一对不同的方法中。



没有内置方式从编译器或IDE获取消息,表明类实例实现了IDisposable - 尽管它将是一个有趣的Visual studio插件,我是不确定实施起来有多简单......

Will do much the same thing, but could be "dispersed" across a couple, of separate methods.

There is no "built in" way to get a message from the compiler or IDE which indicates that a class instance implements IDisposable - though it would be an interesting Visual studio addon, I'm not sure how simple it would be to implement...


这篇关于与使用块战斗,寻找一种安全的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆