处置对象 [英] disposing object

查看:90
本文介绍了处置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我怎样才能告诉我在C#中使用''new''keyward的连续

创建了多少个Class实例?


欢呼


Zest

解决方案

Zest4Csharp写道:< blockquote class =post_quotes>大家好,
我如何知道在C#中使用'new''keyward的连续使用创建了一个类的实例?
<欢呼

Zest




没有内置方式,你可以有一个静态创建计数,但为什么?


JB


谢谢John,

我想我应该已经解释了我现在要做的问题的根源。

好​​的,这是我正在处理的场景:我有Class1创建一个Class2的

实例,如下面的代码行所示:


Class2 MyChild = new Class2;


Class2在一个外部DLL中定义用C#编写。


现在,MyChild将提示用户输入用户ID(这是一个公共财产,在
Class2中)。我希望Class1能够''意识到''MyChild已经捕获了所需的

信息,然后才能去做其他事情。


我在想如果我可以获得新创建的对象的''数组'

那么我可以一次检查它们以查看信息是否被捕获。

否则如何将Class1''知道''Class2发生了什么?


我希望我现在有意义了!


干杯

Zest

===


" John B"写道:

Zest4Csharp写道:

大家好,
我怎么知道我用连续的
在C#中使用''new''keyward?

Zest



没有内置方式,你可能有静态创建计数,但为什么?

JB



Zest4Csharp< Ze ********* @ discussions.microsoft.com>写道:

我想我应该已经解释了我现在要做的问题的来源。
好的,这是我正在处理的场景:我有Class1创建一个
Class2的实例,如下面的代码行:

Class2 MyChild = new Class2;

Class2在用C#编写的外部DLL中定义。

现在,MyChild将提示用户输入用户ID(这是Class2中的公共属性)。我希望Class1能够意识到MyChild已经捕获了所需的信息,然后才能去做其他事情。

我在想,如果我能得到一个''数组''新创建的对象
然后我可以一次检查它们以查看信息是否被捕获。
否则Class1将如何知道Class2发生了什么?



也许你应该给Class2一个Class1可以订阅的事件

to。然后当Class2做了它需要做的任何事情时,它可以提升

事件告诉Class1。


-

Jon Skeet - < sk *** @ pobox.com>
http: //www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


hello everyone,
How can i tell how many instances of a Class have i created with successive
uses of the ''new'' keyward in C#?

cheers

Zest

解决方案

Zest4Csharp wrote:

hello everyone,
How can i tell how many instances of a Class have i created with successive
uses of the ''new'' keyward in C#?

cheers

Zest



No in-built way, you could have a static creation count but why?

JB


Thanks John,
I think i should have explained the source of the problem which i will do now.
Ok, here is the scenario i am dealing with: I have Class1 creating an
instance of Class2 as in the following line of code:

Class2 MyChild = new Class2;

Class2 is defined in an exteranl DLL written with C# as well.

Now, MyChild will prompt the user for user id (which is a public property in
Class2). I want Class1 to be ''aware'' that MyChild has captured the required
info before it can go and do other stuff.

I was thinking that if i can get an ''array'' of the newly created objects
then i can inspect them on at a time to see if the information was catured.
Otherwise how will Class1 ''know'' what has happened to Class2?

I hope i am making sense now!

Cheers
Zest
===

"John B" wrote:

Zest4Csharp wrote:

hello everyone,
How can i tell how many instances of a Class have i created with successive
uses of the ''new'' keyward in C#?

cheers

Zest



No in-built way, you could have a static creation count but why?

JB



Zest4Csharp <Ze*********@discussions.microsoft.com> wrote:

I think i should have explained the source of the problem which i will do now.
Ok, here is the scenario i am dealing with: I have Class1 creating an
instance of Class2 as in the following line of code:

Class2 MyChild = new Class2;

Class2 is defined in an exteranl DLL written with C# as well.

Now, MyChild will prompt the user for user id (which is a public property in
Class2). I want Class1 to be ''aware'' that MyChild has captured the required
info before it can go and do other stuff.

I was thinking that if i can get an ''array'' of the newly created objects
then i can inspect them on at a time to see if the information was catured.
Otherwise how will Class1 ''know'' what has happened to Class2?



Perhaps you should give Class2 an event which Class1 could subscribe
to. Then when Class2 has done whatever it needs to do, it could raise
the event to tell Class1 that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于处置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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