• 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.
  • Posts

    • A summary of the article shouldn't be an issue in theory, as the information is all based within the document itself. The problem with a lot of AI models is that they are trained off both Wikipedia as a primary source and other secondary websites which have less reliable information. As an encyclopaedia it is no doubt going to have bias in one way or another, all encyclopaedias do. The difference is that there is a process for a lot of this information to be figured out whether or not it is an opinion, or the source of that information, and to be aware of the possible bias in the first place, even your own bias as a reader. The way many people understand languages these days is quite limited, particularly in certain areas of the world. This is obvious in many types of "journalism" where some articles are thinly disguised opinion pieces, Where language is carefully curated to push a point of view despite it looking like fact. This is partly why a lot of the Western world is now so divided.
    • I paid for a year subscription, starting in December, I think. I like the user-friendliness and assets like royalty-free music and sounds. But they need to fix some of the horrific bugs before they start adding more features. For starters, you should be able to drag stuff around the timeline without it completely obliterating transitions, synchronization timing, and clip lengths. If you already set up footage and audio how you want, and try to insert more by sliding content to the right, you'll have to clean it all up again. The scroll wheel constantly stops working, and you have to minimize the window and restore it to get it back. When selecting text at the top right in special text tools, it glitches and goes to "copy" mode with a weird pop-up on top of your cursor and you can't type. These are more important than AI tools and new features. Microsoft, start by making your product less painful to use.
    • 🙄 Whats the need to launder? Do you have any reputable sources?
    • Thanks, I'll download it and see how it goes. Gonna be tough. I've used Nova Launcher for around 10+ years I think.
    • Wikipedia has become hot molasses with mostly filth masquerading as truth. Most of it is the imagination of few writers who think of themselves as above god. The narrative setup is mind boggling.
  • Recent Achievements

    • Apprentice
      Adrian Williams went up a rank
      Apprentice
    • Reacting Well
      BashOrgRu earned a badge
      Reacting Well
    • Collaborator
      CHUNWEI earned a badge
      Collaborator
    • Apprentice
      Cole Multipass went up a rank
      Apprentice
    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      ATLien_0
      263
    3. 3
      +Edouard
      191
    4. 4
      +FloatingFatMan
      176
    5. 5
      snowy owl
      133
  • Tell a friend

    Love Neowin? Tell a friend!