C# 我可以取消另一个类中的对象吗
我有一个在一个类中生成的对象,
public class CreatingClass
{
public T CreateObject<T>(Dictionary<string, object> parameters) where T : IMyInterface, new()
{
....
}
public void DestroyObject(IMyInterface objectToDestroy)
{
....
}
}
我从客户端类调用此函数,然后有时需要通过创建类通过我的应用程序将其无效。
我可以做类似以下的事情吗
public class ClientClass
{
MyObject obj;
CreatingClass creatingClass = new CreatingClass();
private void AFunctionToCreateMyClass()
{
obj = creatingClass.CreateObject<MyClass>(parameters);
}
private void AFunctionToDeleteMyObject()
{
CreatingClass.DestroyObject(obj);
Assert.IsNull(obj);//Doesn't fail
}
}
我尝试过 objectToDestroy = null,但认为它不起作用(而且它没有)
I have an object that is generated in one class
public class CreatingClass
{
public T CreateObject<T>(Dictionary<string, object> parameters) where T : IMyInterface, new()
{
....
}
public void DestroyObject(IMyInterface objectToDestroy)
{
....
}
}
I call this function from a client class, then at times need to nullify it through my application by the creating class.
Can I do something like the following
public class ClientClass
{
MyObject obj;
CreatingClass creatingClass = new CreatingClass();
private void AFunctionToCreateMyClass()
{
obj = creatingClass.CreateObject<MyClass>(parameters);
}
private void AFunctionToDeleteMyObject()
{
CreatingClass.DestroyObject(obj);
Assert.IsNull(obj);//Doesn't fail
}
}
I had tried objectToDestroy = null, but didn't think it would work (and it didn't)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对所提供的答案进行一些澄清...
C# 中的参数按其类型、值类型的值、引用类型的引用传递。 然而,您传递的接口可能与类(引用类型)或结构(值类型)相关,因此您需要将其显式声明为 ref 变量。
然而,您真正应该遵循的(如前所述)是 IDisposable 模式,它是为此功能而设计的。
编辑:参数按值传递,但对于引用类型,引用就是值。 因此,即使您破坏函数中的引用,原始引用类型也不受影响。 如果更改引用变量,即修改数据集中的数据,原始引用类型将在函数外部更新。
A little clarification on the answers supplied...
Parameters in C# are passed by their type, value for value types, reference for reference types. You however, are passing an interface which could relate to a class (reference type) or struct (value type), therefore you need to explicitly declare it as a ref variable.
However, what you should really follow (as mentioned previously) is the IDisposable pattern, which was designed for this functionality.
EDIT: Parameters are passed by value, but for reference types the reference is the value. Therefore is you destroy the refence in the function, the original reference type is unaffected. If you make a change to the reference variable, i.e. modifying data in a dataset, the original reference type is updated outside of the function.
[你的“CreatingClass”通常被称为工厂]
不知道为什么你会担心对象的无效化; 只有在所有对它的“根”引用都被删除后,它才会被垃圾收集。 但如果你更改为:
并调用为:
[Your 'CreatingClass' is usually termed a factory]
Not sure why you'd be concerned with nullifying an object; it will be garbage collected only after all 'root' references to it have been removed. But if you change to:
and call as:
您是否在寻找 IDisposable 模式?
Are you looking for the IDisposable pattern?
C#中的所有参数都是按值传递的,所以如果你想修改引用本身,你需要使用
ref
关键字来传递它。All parameters in C# are passed by value, so if you want to modify the reference itself, you need to pass it using the
ref
keyword.你想要的是
这会将你的本地引用设置为空
What you want is
This is will set your local reference to null
请注意,您实际上无法销毁对象;您只能销毁对象。 您必须遵守垃圾收集规则。 在推送时,您可以检查
IDisposable
并调用 Dispose(),您可以使用提供的
ref
示例,但我不确定有什么意义; 使用“obj = null;”清除该字段会更简单。ref
的用法可能会令人困惑,因为它适用于变量 - 即如果您这样做:那么
obj
仍将是原始值。 除非您有充分的理由,否则我不推荐使用ref
方法。Note that you can't actually destroy an object; you are subject to the rules of garbage collection. At a push, you could check for
IDisposable
and call Dispose(),You can use the
ref
samples provided, but I'm not sure there is much point; it is simpler just to clear the field with "obj = null;".The
ref
usage could get confusing, since that works on the variable - i.e. if you do:then
obj
will still be the original value. Unless you have a good reason, I don't recommend theref
approach.