CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2007
    Posts
    5

    Xml-serilizing with interfaces

    A list of interfaces which is implemented by an abstract klass with two subclasses that need to be serialised. I have no idea how to do this since interfaces can not be serialised as I understand it.
    My code generates the error message:

    Cannot serialize interface ConsoleApplication4.Creature_interface.


    ..allthough Im not trying to serilize the interfaces themselves.

    Which is the right way to deal with a problem like this? Is there any stright
    way to deal with this problem or is there some kind of workaround needed?



    Code:
    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using UtilitiesLibrary; 
    using System.Xml.Serialization; 
    
    namespace ConsoleApplication4 
    { 
        class Program 
        { 
            static void Main(string[] args) 
            { 
                Cat cat1 = new Cat("Knut", 6); 
                Dog dog1 = new Dog("Bertil", 5); 
    
                List<Creature_interface> Animallist = new List<Creature_interface>(); 
                Animallist.Add(cat1); 
                Animallist.Add(dog1); 
    
                UniversalSerializer.XMLSerialize("filename.xml", Animallist); 
                Console.ReadLine(); 
                 
            } 
        } 
    
        public interface Creature_interface 
        { 
            string Name { get;} 
            int Age { get; } 
        } 
    
        public abstract class Creature : Creature_interface 
        {    
            string _name; 
            int _age; 
    
            public Creature(string name, int age ) 
            { 
                _name = name; 
                _age = age; 
            } 
    
            public Creature() 
            { } 
             
            public string Name 
            { 
                get { return _name; } 
                set { _name = value; } 
            } 
    
            public int Age 
            { 
                get { return _age; } 
                set { _age = value; } 
            } 
    
            public abstract void Speak(); 
        } 
    
        [Serializable] 
        public class Dog:Creature 
        {    
            public Dog 
                (string _name, int _age): base (_name, _age) 
            { } 
    
            public Dog { } 
    
            public override void Speak() 
            { 
                Console.WriteLine("Bark Bark"); 
            } 
        } 
    
        [Serializable] 
        public class Cat:Creature 
        { 
            public Cat 
                (string _name, int _age): base (_name, _age) 
            { } 
    
            public Cat  { } 
    
    
    
            public override void Speak() 
            { 
                Console.WriteLine("Mjau Mjau"); 
            } 
        } 
    
    }
    Code:
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.IO;  
    using System.Xml.Serialization;  
    using System.Runtime.Serialization.Formatters.Binary;  
    
    namespace UtilitiesLibrary  
    {     
        public class UniversalSerializer  
        {  
    
            public static void XMLSerialize<T>(string filename, T objectToSerialize)  
            {     
                 XmlSerializer Serializer = new XmlSerializer(typeof(List<Creature_interface>), new Type[] {typeof(Dog), typeof(Cat)});
    
                 Stream fStream = new FileStream(filename, FileMode.Create,  
                 FileAccess.Write, FileShare.None);  
    
                 Serializer.Serialize(fStream, objectToSerialize);  
                 fStream.Close();  
            }  
    //more methods...  
        }  
    }
    Thank you for any help.
    Last edited by grignard; January 10th, 2008 at 12:49 PM.

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Xml-serilizing with interfaces

    since interface cannot be serialized, have you ever tried the base class - List<Creature>?? think it should work. furthermore, you must add a parameterless constructor to your Dog and Cat class.
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured