C# 我可以取消另一个类中的对象吗

发布于 2024-07-11 12:49:24 字数 816 浏览 5 评论 0原文

我有一个在一个类中生成的对象,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

三寸金莲 2024-07-18 12:49:26

对所提供的答案进行一些澄清...

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.

一生独一 2024-07-18 12:49:26

[你的“CreatingClass”通常被称为工厂]

不知道为什么你会担心对象的无效化; 只有在所有对它的“根”引用都被删除后,它才会被垃圾收集。 但如果你更改为:

 public void DestroyClass(ref IMyInterface objectToDestroy)   
 {       
     ....    
     objectToDestroy = null;
 }

并调用为:

private void AFunctionToDeleteMyObject()    
{        
   CreatingClass.DestroyObject(ref obj);    
   Assert.IsNull(obj);  
}

[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:

 public void DestroyClass(ref IMyInterface objectToDestroy)   
 {       
     ....    
     objectToDestroy = null;
 }

and call as:

private void AFunctionToDeleteMyObject()    
{        
   CreatingClass.DestroyObject(ref obj);    
   Assert.IsNull(obj);  
}
七度光 2024-07-18 12:49:26

您是否在寻找 IDisposable 模式

Are you looking for the IDisposable pattern?

诺曦 2024-07-18 12:49:26

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.

自由如风 2024-07-18 12:49:25

你想要的是

public void DestroyClass(ref IMyInterface objectToDestroy)
{
    ....
    objectToDestroy = null;
}

这会将你的本地引用设置为空

What you want is

public void DestroyClass(ref IMyInterface objectToDestroy)
{
    ....
    objectToDestroy = null;
}

This is will set your local reference to null

浊酒尽余欢 2024-07-18 12:49:25

请注意,您实际上无法销毁对象;您只能销毁对象。 您必须遵守垃圾收集规则。 在推送时,您可以检查 IDisposable 并调用 Dispose(),

您可以使用提供的 ref 示例,但我不确定有什么意义; 使用“obj = null;”清除该字段会更简单。

ref 的用法可能会令人困惑,因为它适用于变量 - 即如果您这样做:

var tmp = obj;
DestroyObject(ref tmp);

那么 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:

var tmp = obj;
DestroyObject(ref tmp);

then obj will still be the original value. Unless you have a good reason, I don't recommend the ref approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文