Ah, Frans, you're right!

I seem to have forgotten a case which taught me that the .net
framework *can* instantiate classes that have parameterized ctors, the
catch is, however, that the framework will not call the ctors, not
even the parameterless ctor...

I was assigned to fix a NullRef exception bug on usage of a certain
class which was similar to this code (extremely simplified)

[Serializable]
class Foo
{
 ...
 object bar;
 public Foo() { bar = new object(); }
 public object Bar { get { return bar; } }
}

The exceptions were occurring when using the Bar property. I could not
believe my eyes. How on earth could the returned bar be null?!? *No*
other code was changing the value of bar!!...

My world started to collapse until I found out the exceptions were
only occurring on deserialized instances... That's where I saw the
attribute (which I deliberately excluded from previous code)...

[Serializable]
class Foo
{
 [NonSerialized]
 object bar;
 public Foo() { bar = new object(); }
 public object Bar { get { return bar; } }
}

The value of bar was not serialized! But... it was also not renewed on
deserialization!

The framework is capable of creating instances at an uninitialized
state, all the fields of the instance have the value 0 for valuetypes
and null for referencetypes, without calling any ctor. Since bar was
not being serialized, on deserialization it kept the uninitialized
value of null, hence the NullRef exceptions.

Somehow this case was unconsciously driven into the darker areas of my memory...
Thanks for putting me back on track!

// Ryan

On 10/18/06, Frans Bouma <[EMAIL PROTECTED]> wrote:
> On 10/18/06, Manoj Aggarwal <[EMAIL PROTECTED]> wrote:
> > Can you please explain why is it so...
> > Also It will be helpful if u can explain what is "datarow with
> > parameterless constructor", an example will help me a long way
>
> The .net framework must be able to construct a class from it
> serialized form.
> In other words the class must be deserializable.
> Therefor it mandate the class to have a parameterless constructor.
>
> class Foo
> {
> public Foo() { /* do something neat */ } }
>
> class Bar
> {
> public Bar(int value) { /* do something neat */ } }
>
> class Bar doesnt have a parameterless ctor, it wants an int.
> The .net framework cannot simple know what value it should
> pass, therefor it can deserialize class Foo but not class Bar.

       sure it can, though the XmlSerializer can't (at least in .net 1.x). The 
remoting formatters can. I have classes marked with
[Serializable] and with solely parameter using constructors. And they serialize 
fine (without ISerializable requirements).

       As the TS talked about webservices, I think it's good to distinguish 
remoting serialization and xml serialization as these
are not related :)

               FB

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to