恢复对象的价值 [英] recuperate value for object

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

问题描述





我想执行两个查询,之后,我想返回一个对象文章并将其用于其他处理

我的功能是:

文章文章=  Article(); 
modelTest.GetQuantite(IDArt,(Qantite)= >
{
model.GetPrix(IDArt,Date,(res,错误)= >
{
if (错误!= null
{
// TODO: NOTIFY USER
return ;
}
else if (res!= null
{
article.QantiteMagazin = Quantite;
article.Prix = res;
article.Total = Math.Round((Quantite.Value * res.Value), 3 );
}
});
});
返回文章;





我的问题是,当我拿文章.Prix或article.QuantiteMagazin;它是null

解决方案

你正在试图突破门户。 .NET是内存管理系统。您无需执行任何特殊操作即可重用该对象。事实上,它甚至毫无意义。一旦你失去了对某个对象的访问权限(比如,你将唯一的访问权限赋予null;但这仅仅是例如;通常,你永远不需要它),该对象成为垃圾的主体收集,这发生在一段时间后,不在你的控制之下。



你需要了解引用,引用类型变量或成员不是与它们引用的对象相同的对象。您可以简单地将引用变量/成员分配给某个新对象,它将使您之前引用的对象从您的访问中丢失(如果您没有通过其他引用继续访问它)。这里解释了这个机制: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]。



只有你应该更加小心一些对象,那些具有实现 System.IDisposable 的运行时类型的对象。对于他们来说,在忘记它们之前调用 System.IDisposable.Dispose 至关重要。例如,如果不这样做,可能会导致无法管理的内存泄漏以及其他令人不快的后果。一般情况下,这种处理与垃圾收集和管理内存,甚至任何其他内存无关;它用于确保任何类型的清理行动。请参阅:

https:/ /msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx [ ^ ],

参见:https://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



尽管如此,管理内存泄漏是非常可能的,但它们可能是由于代码设计错误而不是偶然发生的。请查看我过去的答案:

WPF DataBinding中的内存泄漏 [ ^ ],

MDI表格中的内存管理 [ ^ ],

摆脱公众的最佳途径静态列表导致内存不足 [ ^ ]。



-SA

hi,

I want to execute two queries, after that, I want to return an object "article" and use it for other treatments
my function is:

 Article article = new Article();
modelTest.GetQuantite(IDArt, (Qantite) =>
             {
               model.GetPrix(IDArt, Date, (res, err) =>
               {
                if (err != null)
                {
                    // TODO: NOTIFY USER
                    return;
                }
                else if (res != null)
                {
                    article.QantiteMagazin = Quantite;
                    article.Prix = res;
                    article.Total = Math.Round((Quantite.Value * res.Value), 3);
                }
              });
            });
            return article;



my issue is that when i take article.Prix or article.QuantiteMagazin; it's null

解决方案

You are trying to "break through the open door". .NET is memory managed system. You don't need to do anything special to reuse the object. In fact, it even makes no sense. As soon as you loose the access to some object (say, you assign the only reference giving access to it to null; but this is only for example; normally, you never need it), the object becomes the subject to garbage collection, which happens some time later, which not under your control.

You need to understand that references, reference-type variables or members are not the same objects as the objects they reference. You can simply assign the reference variable/member to some new object, and it will make the previously referenced object lost from your access (if you did not keep access to it through some other references). Here the mechanism is explained: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Only you should be more careful with some objects, those which have the runtime types implementing System.IDisposable. For them, it is critically important to call System.IDisposable.Dispose before you "forget" them. Failure to do so may lead, for example, to unmanaged memory leaks, and other unpleasant consequences. In general case, this disposal is unrelated to garbage collection and managed memory, or even any other memory; it is used to ensure any kind of clean-up action. Please see:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^],
see also: https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

Nevertheless, manages memory leaks are quite possible, but they can happen due to mistakes in code design rather than by accident. Please see my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^].

—SA


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

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