• 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

    • Good afternoon, I have a customer who purchased a Dell laptop last year. One of the latest Inspirons. they are older so it can be a bit hard to see things on the screen. They are using Windows 11 Home 23H2, I have not upgraded them yet to 24H2. What I did was changed the scaling to 150% and that automatically made all the icons on desktop bigger and using in Chrome webpage elements are bigger etc. The thing is I overlooked the Taskbar and the icons most notably hidden within the arrow pointing up. I though by changing to 150% the scaling even the taskbar would get bigger. Again I don't know how I missed seeing that. Is this something that can be fixed? One other thing is I notice in File Explorer things are also small and not larger after the change. Can this be fixed as well?   Thank You.
    • Apple still has two unannounced features for iOS 26 by Hamid Ganji Apple held its WWDC25 event this month to unveil a slew of AI features and its new design for operating systems, known as Liquid Glass. While iOS 26 currently has no shortage of features, the iPhone maker might still have some features under wraps that didn’t make a debut at this month’s WWDC. Bloomberg’s Mark Gurman writes in his weekly Power On newsletter that Apple didn’t announce two iOS 26 features at the WWDC event. The first feature is a live translation of conversations via AirPods, and the latter is the ability to sync your wireless network login information across devices at a hotel or gym. Even though these features were already tipped to arrive in iOS 26, Apple held them from the event, presumably because they’re still not ready to ship. Apple appears to have learned from its experience with Apple Intelligence and aims to break the habit of unveiling new features before they are ready for release. The Live Translation on iOS 26 is currently integrated with popular apps like Messages, FaceTime, and Phone to help users break language barriers and communicate in different languages. The feature is now under development for AirPods, allowing users to hear real-time speech translations. Moreover, the WiFi syncing feature allows you to sync your sign-in information across your entire Apple ecosystem, enabling you to connect to a public WiFi network with ease. This would eliminate the need to sign into a WiFi network separately on each device. While these features haven’t arrived in iOS 26 yet, they’re more likely to be released later this year, in October or December.
    • Intel Level L4 cache has been around for a long time, so AMD Zen 3D Cache is a copy of Intel L4 cache that was introduced with Broadwell i7-5775c, which had 128mb of Level L4 cache. Secondly, Zen 6 is not faster in single-threaded performance than 13900k/14900k. 285k is something new Intel is trying, basically showing us that chiplet design does not work for gaming, just like AMD design does not work for gaming. Throwing more cores at it and having L4 cache won't fix the issue, and that is frame dip and stuttering caused by a very stupid design decision to keep the memory controller outside the compute tile. Might as well put a memory controller by bringing back a south bridge chip. lol
    • Just like AMD Zen, it is not going to work for gaming despite 3D Cache. Frame dip and stuttering are giving, which renders 14900KS the greatest gaming CPU ever released.
  • Recent Achievements

    • Community Regular
      Primey_ went up a rank
      Community Regular
    • Reacting Well
      Gromvar earned a badge
      Reacting Well
    • Dedicated
      BreakingBenjamin earned a badge
      Dedicated
    • Week One Done
      Hartej earned a badge
      Week One Done
    • One Year In
      TsunadeMama earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      +FloatingFatMan
      181
    3. 3
      ATLien_0
      166
    4. 4
      Skyfrog
      99
    5. 5
      Som
      96
  • Tell a friend

    Love Neowin? Tell a friend!