Home > Net >  This constructor executes?
This constructor executes?

Time:11-13

List _As;
_As=_DbCnn. Query
(SQL). ToList ();//this is a collection of database query out

Object A has A constructor
The public (A)
{
_B=new B ();
}
Excuse me _As=_DbCnn. Query
(SQL). ToList (); This assignment, the object A constructor executes?

CodePudding user response:

You're not yourself next breakpoint??

CodePudding user response:

Does not perform, not instantiate objects, such assignment is an error, not the instance reference to the object,

CodePudding user response:

In principle, if a class can be instantiated smoothly. There must be a constructor to perform, either a parameterless constructor, either has a constructor parameters,

CodePudding user response:

reference 3 floor closure guest response:
according to the truth, if a class can be instantiated smoothly. There must be a constructor to perform, either a parameterless constructor, either has a constructor parameters,


Some ORM framework will avoid the constructor, the intention may be to avoid the additional constructor logic,
Since dotnet1.1, actually can use FormatterServices. Get Uninitialized Object (Type) to create an Object constructor don't use it,
Dotnet Remoting BinaryFormatter also internal use FormatterServices. GetUninitializedObject to ward off the constructor to inverse series of possible side effects,

 using System; 
Using System. IO;
Using System. The Runtime. Serialization;
Using System. The Runtime. Serialization. Formatters. Binary;

Class Program
{
//in Dotnet Framework 4.0 compiler running under the
The static void Main (string [] args)
{
My my1=new My ();//calls the constructor, and the output 'My constructed! Id=123 '

BinaryFormatter bf=new BinaryFormatter ();
MemoryStream ms=new MemoryStream ();
Bf. Serialize (ms, my1);

Ms. Position=0;
My my2=(My) bf. Deserialize (ms).//not calls the constructor
My2. Id=888;
Console. WriteLine (" my1. Id={0}; My2. Id={1} ", my1. Id, my2. Id);
Ms. The Dispose ();



Console. WriteLine (" another way ");
The Console. ReadLine ();

My my3=(My) FormatterServices. GetUninitializedObject (typeof (My));//not calls the constructor, but has been allocated space needed
Console. WriteLine (" my3. Id={0}; ", my3. Id);//my3. Id=0
}
}

[Serializable]
The class My
{
Public My ()
{
Id=123;
Console. WriteLine (" My constructed! Id="+ Id);
}
Public int Id {get; The set; }
}


CodePudding user response:

You're not yourself next breakpoint??

CodePudding user response:

If the business logic is "should call instantiation method", but you find a framework under the breakpoint didn't call instantiation method, so it should be as framework of bugs, abandon it to switch to other frameworks,

CodePudding user response:

BinaryFormatter business logic is very at the bottom of the "copy" of information, this is nothing to do with "applications", but a pure concept in the field of computer,

Assume that your object is in the fields of business, you should let instantiate method implemented, for example A should refer to A B instance object is clearly you want to express the application domain model, rather than A pure concept of 0, 1 in the field of computer data,

CodePudding user response:

Don't call the constructor creates an instance of a class, it will bring a lot of problems, the root cause is against the original intention of the author of the class, the author writing the constructor, the purpose is inevitably calls, so many of them are the constructor which variable initialization,

CodePudding user response:

The constructor will run

CodePudding user response:

Breakpoint debugging

CodePudding user response:

The
refer to 12 floor xiaoxiangqing reply:
constructor will run

By verifying said I will perform, have not against?

CodePudding user response:

By verifying said I will perform, have not against?

CodePudding user response:

To be honest, we also guess,
In general, will be instantiated,
But will not, you hit a breakpoint, to see whether the frame internal will avoid the constructor,
Some architecture will generate their own constructor, because he will aop into some other things,
The obvious is the spring framework,

CodePudding user response:

Before the _B=new B (), add a breakpoint, should stop, or trying to change the constructor to private, compile or runtime will not error,
  •  Tags:  
  • C#
  • Related