传递派生对象作为参考参数 [英] Passing a Derived Object as a Reference Parameter
问题描述
如何创建一个方法,将从
基类派生的任何对象作为参考参数?
这样的东西:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}
DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2) ;
DestroyObject(ref derivedObject3);
其中derivedObject1,2和3属于派生自
BaseObject的类类型?
谢谢....
- 布鲁斯
"布鲁斯" < bv *************** @ mailblocks.com> écritdansle message de news:
LR ******** @ 4ax.com 。 ..
这样的东西:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
OBJ = NULL;
}
DestroyObject(参考文献derivedObject1);
DestroyObject(参考文献derivedObject2);
DestroyObject(参考文献derivedObject3);
是的,但我不得不问你为什么要明确处理对象
让垃圾收集器做它的工作?或者你的课程是否持有
对Windows资源或文件句柄的引用?
Joanna
-
Joanna Carter
顾问软件工程师
" Bruce" < bv *************** @ mailblocks.com> écritdansle message de news:
LR ******** @ 4ax.com 。 ..
这样的东西:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
OBJ = NULL;
}
DestroyObject(参考文献derivedObject1);
DestroyObject(参考文献derivedObject2);
DestroyObject(参考文献derivedObject3);
是的,但我不得不问你为什么要明确处理对象
让垃圾收集器做它的工作?或者你的课程是否持有
对Windows资源或文件句柄的引用?
Joanna
-
Joanna Carter
顾问软件工程师
>其中derivedObject1,2和3属于派生自<的类类型blockquote class =post_quotes> BaseObject?
BaseObject bo = derivedObject1;
DestroyObject(ref bo);
但我不明白你为什么使用ref参数。如果您使用了
值参数,则可以直接传入derivedObject1。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
How can I create a method that will take any object derived from a
base class as a reference parameter?
Something like this:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}
DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);
Where derivedObject1, 2 and 3 are of a class type derived from
BaseObject?
Thanks....
--Bruce
"Bruce" <bv***************@mailblocks.com > a écrit dans le message de news:
lr********************************@4ax.com...
Something like this:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}
DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);
Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?
Joanna
--
Joanna Carter
Consultant Software Engineer
"Bruce" <bv***************@mailblocks.com > a écrit dans le message de news:
lr********************************@4ax.com...
Something like this:
void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}
DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);
Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?
Joanna
--
Joanna Carter
Consultant Software Engineer
>Where derivedObject1, 2 and 3 are of a class type derived fromBaseObject?
BaseObject bo = derivedObject1;
DestroyObject(ref bo);
But I don''t understand why you use a ref parameter. If you used a by
value parameter, you could pass in derivedObject1 directly.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
这篇关于传递派生对象作为参考参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!