var s=new foo();class foo
{
  foo(){ var m=0; m=m/m; }
}一直以为没有被创建对象,今天发现貌似只是阻止了赋值,对象仍然是创建出来了。求确认这么想的原因是,如果我在构造函数里写  Session["test"]=this,然后将   s=new foo() 用try/catch掉异常后,可以通过 Session["test"]得到一个没有被完整构造的foo实例。有点可怕的说

解决方案 »

  1.   

    发现这也给在基类中屏蔽子类构造函数(不管出于什么目的)的方式:基类构造方法的最末尾把this存到静态变量中后抛出异常
      

  2.   

    But here is the other question: how does Thread.Abort work with finalizable objects? As in:
    var s = new StreamReader();
    ...
    what if thread abort happen inside the call to constructor? 

    Constructors are nothing special to the thread manager. They're just methods that happen to be called right after an object is allocated. -- Eric
     
    Will object get to finalazer queue in that case? 
    Of course. It had better -- the constructor might have already allocated objects that need finalization! -- Eric 

    If it does, will that mean that finilizer will see uninitialized object?

    Yes. So you had better make sure that you write your finalizable objects to be robust in the situation where constructors fail half way through. Since you already have to write your finalizers to be robust in the face of multi-threading race conditions, this shouldn't be too much of an additional burden. (Finalizers run on a different thread than the main execution thread, so they have to be thread safe.) Remember, writing finalizable objects is an advanced topic for programmers who know exactly what the runtime finalization semantics are. If you do not know how it works, do what I do. I do not attempt to write a finalizer without help from someone who does understand it. -- Eric