如何处置全局对象 [英] How to dispose global objects

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

问题描述

嘿伙计们,我在其他类中使用类的方法,通过制作像



DO_AddData ObjAddData = new DO_AddData(); ---- Do_AddData其他类

这是在我当前的类中全局定义的。



i在当前类的许多方法中使用ObjAddData。之后我想要处理这些对象。 。



我尝试使用关键字,但这对只有本地范围的本地对象有好处...



我也用过最后试试





试试

{

///////

}



终于

{

((System.Idisposable))。dispose();



}

但它会抛出像......这样的异常。



无法将'Penmia.Objects.DO_AddData'类型的对象强制转换为'System.IDisposable'。



那么有没有适合处理全球物体的场景。



请提供任何lution

感谢您的问候

Rahul

}

Hey guys I am using methods of class in other class by making objects like

DO_AddData ObjAddData = new DO_AddData();----Do_AddData other class
this is defined globally in my current class.

i use ObjAddData in many methods of current class .After that i want to dispose this objects..

I try "using" keyword but it's good for local objects which has local scope only...

I also used try finally like


try
{
///////
}

finally
{
((System.Idisposable)).dispose();

}
but it throws exceptions like...

"Unable to cast object of type 'Penmia.Objects.DO_AddData' to type 'System.IDisposable'."

So is there any suitable scenerio for disposing global objects.

Please provide any solution
thank n regards
Rahul
}

推荐答案

无效的问题和纯粹是人为的问题​​。请看我对这个问题的评论。



你永远不应该投射到 IDisposable 。如果某些类型确实实现了这个接口, Dispose 调用将不需要这种情况,如果没有,对象根本不需要处理



我不知道你是否明白这个界面通常与回收托管内存无关。在对象变得无法访问之后,垃圾收集器完成此类回收。所以,以防万一,请阅读它: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science %29 [ ^ ]。< br $>


-SA
Invalid question and purely artificial "problem". Please see my comment to the question.

You never ever should cast to IDisposable. If some type does implements this interface, the Dispose call won't need the case, if not, the object don't need to be disposed at all.

I don't know if you understand that this interface is generally unrelated to reclaiming of managed memory. Such reclaiming is done by the Garbage Collector after objects become unreachable. So, just in case, please read about it: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

—SA


HI


试试喜欢这...







HI
try like this...



public class OtherClass
      {
          public void somemethod()
          {

              using (DO_AddData obj = new DO_AddData ())
              {

// ur coding here...

              }

          }
      }



      public class DO_AddData : IDisposable
      {
          public void Dispose()
          {
             //
          }
      }









有关使用声明的更多信息,请参阅浏览此链接



http: //msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx [ ^ ]


尝试此解决方案..



try this solution..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace POC
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        DO_AddData obj = new DO_AddData();
        protected void Page_Load(object sender, EventArgs e)
        {
            somemethod();
            method2();
        }
        public void somemethod()
        {
            using (obj)
            {
                string str = "hello";
                obj.insert(str);
            }

        }
        public void method2()
        {
            using (obj)
            {
                string str = "hEY";
                obj.insert(str);
            }

        }
    }

    public class DO_AddData : IDisposable
    {
        public void insert(string STRPARAM)
        {
            /////insert in database
        }

        public void Dispose()
        {
            // dispose all the objects used in this class
        }
    }
}


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

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