• 0

<C#> Inheriting from List<>


Question

I am trying to inherit from List<Blah> so essentially I have something like this:

class Foo : List<Blah>

{

...

private string _var1;

private string _var2;

public string Var1

{

get { return _var1; }

}

public string Var2

{

get { return _var2; }

}

...

}

Now the problem is when I go to serialize this I get back only a representation of the inherited List properties (the collection of Blah objects) and nothing in the Foo class like Var1 and Var2. If I pull the List<Blah> down into the class instead of inheriting from it, there are no issues. What is causing the serializer to miss the public properites of the Foo class?

Thanks and regards

Link to comment
https://www.neowin.net/forum/topic/496842-c-inheriting-from-list/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

First, what serializer are you using? XML? Binary?

If you're using an XmlSerializer, have you tried adding serialization attributes to your properties? The serializer may not serialize get-only properties by default:

[XmlElement("Var1Element")]
public String Var1
{
   get {return _var1;}
}

If you're using a binary serializer, you need to mark your class (inheriting from a serializable base class may not good enough) with the SerializableAttribute, and inherit from the ISerializable interface if you require control over serialization.

  • 0
  dannysmurf said:

First, what serializer are you using? XML? Binary?

If you're using an XmlSerializer, have you tried adding serialization attributes to your properties? The serializer may not serialize get-only properties by default:

[XmlElement("Var1Element")]
public String Var1
{
   get {return _var1;}
}

If you're using a binary serializer, you need to mark your class (inheriting from a serializable base class may not good enough) with the SerializableAttribute, and inherit from the ISerializable interface if you require control over serialization.

Thanks for your reply. I am using the XmlSerializer and although I did not place a set in the example code, there is indeed one there. Applying the XmlElement attribute was unsuccessful.

Complete example non-working code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

public class Foo : List&lt;Blah&gt;
{
	public Foo()
	{
	}

	public Foo(string var1, string var2)
	{
		_var1 = var1;
		_var2 = var2;
	}

	private string _var1;
	private string _var2;

	[XmlElement("Var1")]
	public string Var1
	{
		get { return _var1; }
		set { _var1 = value; }
	}

	[XmlElement("Var2")]
	public string Var2
	{
		get { return _var2; }
		set { _var2 = value; }
	}

	public string ToXML()
	{
		return UtilTesting.GetXML(this);
	}
}

public class Blah
{
	public Blah()
	{

	}

	public Blah(string word, int number)
	{
		_word = word;
		_number = number;
	}

	private string _word;
	private int _number;

	public string Word
	{
		get { return _word; }
		set { _word = value; }
	}

	public int Number
	{
		get { return _number; }
		set { _number = value; }
	}
}

public class UtilTesting
{
	public static string GetXML(object obj)
	{
		XmlDocument retVal = new XmlDocument();
		//Convert the obj into an Xml document
		XmlSerializer ser = new XmlSerializer(obj.GetType());
		StringBuilder sb = new StringBuilder();
		TextWriter writer = new StringWriter(sb);
		ser.Serialize(writer, obj);

		return sb.ToString();
	}
}

Use it as follows:

   Foo test = new Foo("Test", "Testing");
   test.Add(new Blah("One", 1));
   test.Add(new Blah("Two", 2));
   string xml = test.ToXML();

  • 0

I found this on MSDN for the XmLSerializer class.

  Quote
Only collections are serialized, not public properties.

I dont really understand it because when i take out the List<Blah> inherit from foo and the " test.Add(new Blah("One", 1));" in main i get

&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">lt;Var1&gt;Test&lt;/Var1&gt;
  &lt;Var2&gt;Testing&lt;/Var2&gt;
&lt;/Foo&gt;

..... might want to post this in the MSDN forms

  • 0
  dolimite35 said:

I found this on MSDN for the XmLSerializer class.

I dont really understand it because when i take out the List<Blah> inherit from foo and the " test.Add(new Blah("One", 1));" in main i get

&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">lt;Var1&gt;Test&lt;/Var1&gt;
  &lt;Var2&gt;Testing&lt;/Var2&gt;
&lt;/Foo&gt;

..... might want to post this in the MSDN forms

Thanks for you input. I have made a similar post on the MSDN forums...for anyone interested: http://forums.microsoft.com/MSDN/ShowPost....46&SiteID=1

  • 0

I found an answer although I wish it was a better one. Basically when inheriting from a list or any collection for that matter, public properties are not serialized because the collection is treated as an array and not an object. The article can be found here: http://forums.microsoft.com/MSDN/ShowPost....46&SiteID=1

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.