views:

256

answers:

2

Hi all,

I'm working on a project that involves some home-made (de-)serialization code, and I have run into some issues with the deserializing of a List. The following code is throwing a NullReferenceException on the second line, even though the var list is not null, and happily reports Count = 0 when hovering my mouse over it.

System.Collections.IList list = ((System.Collections.IList)obj);
list.Add(val);

My variables are being instantiated a bit weird, so maybe that has got something to do with it? This is the code involved.

System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);

Where type is

System.Type.GetType("System.Collections.Generic.List`1[[Networking.Client, Networking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Any help would be much appreciated :)

A: 

Is your val instantiated properly? That's the only thing I can see being a problem here, but it's tough to say.

Andy Mikula
+2  A: 

Bah, I hate finding an answer just after I asked a question about it. As it turns out List<T> does not like to be instantiated using GetUninitializedObject, so I had to add a special case for Lists to use Activator.CreateInstance (which is not an option for most classes).

Aistina