• 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

    • As it would on any other tech website whether it be Windows, Apple, etc. I wouldn’t expect it any other way.
    • Mozilla should shut down everything except the browser and the services they whitelabel (for example Firefox VPN from Mullvad). Everything else just cost them money and have never returned any revenue. Focusing in the browser is what makes sense considering what is going to change regarding their income sources. About Firefox and chromium... if they ever have to abandon Gecko because of financial issues it is more likely to use Apple's WebKit and not Chromium and Blink. WebKit is more versatile for Firefox because it will allow them even to keep Firefox's extension ecosystem. The same way GNOME Web which is a WebKit browser has experimental Firefox extension support.
    • I really dont like how they’ve put shadows under window elements making the sidebar and toolbar buttons look like they sit above the window itself. Looks very odd and clunky.
    • Rubbish. It's chock-full of ads and with manifest 3.0,the gloves are off. Google does not even pretend now. They want évil and want your data
    • Glary Utilities 6.27.0.31 by Razvan Serea Glary Utilities offers numerous powerful and easy-to-use system tools and utilities to fix, speed up, maintain and protect your PC. Glary Utilities allow you to clean common system junk files, as well as invalid registry entries and Internet traces. You can manage and delete browser add-ons, analyze disk space usage and find duplicate files. You can also view and manage installed shell extensions, encrypt your files from unauthorized access and use, split large files into smaller manageable files and then rejoin them. Furthermore, Glary Utilities includes the options to find, fix, or remove broken Windows shortcuts, manage the programs that start at Windows startup and uninstall software. All Glary Utilities tools can be accessed through an eye-pleasing and totally simplistic interface. Glary Utilities 6.27.0.31 changelog: Optimized Disk Cleaner: Optimized the scanning algorithm for Firefox to enhance user experience. Optimized Disk Cleaner: Added support for Nitro Pro 14. Optimized Tracks Eraser: Optimized the scanning algorithm for Firefox to enhance user experience. Optimized Tracks Eraser: Added support for Nitro Pro 14. Optimized Startup Manager: Optimized the review algorithm to enhance user experience. Optimized Uninstaller Manager: Optimized the scanning algorithm to enhance user experience. Minor GUI improvements. Minor bug fixes. Download: Glary Utilities 6.27.0.31 | 27.0 MB (Freeware) Download: Portable Glary Utilities | 32.3 MB View: Glary Utilities Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Æhund earned a badge
      One Month Later
    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
    • Explorer
      MusicLover2112 went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      ATLien_0
      269
    3. 3
      +FloatingFatMan
      242
    4. 4
      +Edouard
      202
    5. 5
      snowy owl
      168
  • Tell a friend

    Love Neowin? Tell a friend!