Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 7:29
professionalEddy Vluggen14-Sep-15 7:29 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
OriginalGriff14-Sep-15 8:09
mveOriginalGriff14-Sep-15 8:09 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 9:26
mveGerry Schmitz14-Sep-15 9:26 
JokeRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 9:38
professionalEddy Vluggen14-Sep-15 9:38 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 9:54
mveGerry Schmitz14-Sep-15 9:54 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 10:05
professionalEddy Vluggen14-Sep-15 10:05 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 10:29
mveGerry Schmitz14-Sep-15 10:29 
QuestionSerializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff14-Sep-15 5:06
professionalBillWoodruff14-Sep-15 5:06 
I've had no problem in the past using WCF's DataContract/DataMember Attributes and serializing to XML with complex objects, including generic Dictionaries. But, after a few hours of reading up on WCF serialization, and reading posts on the usual places, I am stuck.

Here's a (down to the bare-bones for the sake-of keeping this short, .NET 4.5 code) example illustrating what I am trying to do.
C#
using System;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.IO;

namespace WhatEver
{
    public interface ISomeInterface
    {
        DateTime Date { set; get; }
    }

    public class BunchOfISomeInterface : Dictionary<Type, List<ISomeInterface>>
    {
    }

    [DataContract]
    public classLite : ISomeInterface
    {
        [DataMember]
        public DateTime Date { set; get; }
    }

    [DataContract]
    public classRegular : ISomeInterface
    {
        [DataMember]
        public DateTime Date { set; get; }

        [DataMember]
        public extraString { set; get; }
    }

    public static class WriteMe
    {
        private static DataContractSerializer serializer;

        static string filepath = @"C:\test.xml";

        public static void Write(BunchOfISomeInterface bunch)
        {
            using (FileStream writer = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                serializer = new DataContractSerializer(typeof(BunchOfISomeInterface));

                // error here
                serializer.WriteObject(writer, bunch);
            }
        }
    }
}
If you call/use the above code using this in some .NET Class:
C#
using WhatEver;

private void SomeTestClass()
{
    BunchOfISomeInterface currentBunch = new BunchOfISomeInterface();
    
    Lite lite1 = new Lite();
    lite1.Date = DateTime.Now;

    Regular regular1 = new Regular();
    regular1.Date = DateTime.Now;
    regular1.extraString = "extra";

    currentBunch.Add(0, new List<ISomeInterface>());
    currentBunch[0].Add(lite1);

    currentBunch.Add(1, new List<ISomeInterface>());
    currentBunch[1].Add(regular1);

    WriteMe.Write(currentBunch);
}
You'll get this error:

"An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll
Additional information: Type 'WhatEver.Lite' with data contract name"

I've tried several things here:

1. putting the [DataContract] Attribute on BunchOfISomeInterface: that gives an error that Bunch... cannot be serializable.

2. putting [DataContract] [KnownType(typeof(BunchOfISomeInterface))] Attributes on WriteMe: doesn't change the error.

3. I tried changing the ISomeInterface to be a Class rather than an interface: same error; that surprised me:
C#
[DataContract]
public class ISomeInterface
{
    [DataMember]
    public DateTime Date { set; get; }
}
That results in the same error message.

This thread on StackOverFlow suggests that you can't serialize Interfaces: [^], but I am not at a level with C# that I can grok if that's relevant here.

In other experiments I am getting the error: "Type 'System.RuntimeType' with data contract name"

thanks, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.


modified 14-Sep-15 11:18am.

QuestionRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Eddy Vluggen14-Sep-15 7:32
professionalEddy Vluggen14-Sep-15 7:32 
AnswerRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff15-Sep-15 6:42
professionalBillWoodruff15-Sep-15 6:42 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Eddy Vluggen15-Sep-15 7:55
professionalEddy Vluggen15-Sep-15 7:55 
AnswerRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Bernhard Hiller15-Sep-15 5:44
Bernhard Hiller15-Sep-15 5:44 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Richard Deeming15-Sep-15 5:58
mveRichard Deeming15-Sep-15 5:58 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff16-Sep-15 0:22
professionalBillWoodruff16-Sep-15 0:22 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Richard Deeming16-Sep-15 2:04
mveRichard Deeming16-Sep-15 2:04 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff16-Sep-15 7:37
professionalBillWoodruff16-Sep-15 7:37 
GeneralRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff15-Sep-15 6:58
professionalBillWoodruff15-Sep-15 6:58 
QuestionGridView Export to Excel keeping the date format Pin
Member 1180079714-Sep-15 3:34
Member 1180079714-Sep-15 3:34 
AnswerRe: GridView Export to Excel keeping the date format Pin
Richard MacCutchan14-Sep-15 6:31
mveRichard MacCutchan14-Sep-15 6:31 
QuestionWhat is it like working as a .NET developer? Pin
Jason Smith14-Sep-15 0:54
Jason Smith14-Sep-15 0:54 
AnswerRe: What is it like working as a .NET developer? Pin
Richard MacCutchan14-Sep-15 1:01
mveRichard MacCutchan14-Sep-15 1:01 
GeneralRe: What is it like working as a .NET developer? Pin
Jason Smith14-Sep-15 1:36
Jason Smith14-Sep-15 1:36 
QuestionHow to change a textbox text while the code is running Pin
Member 1191673513-Sep-15 20:29
Member 1191673513-Sep-15 20:29 
AnswerRe: How to change a textbox text while the code is running Pin
OriginalGriff13-Sep-15 21:03
mveOriginalGriff13-Sep-15 21:03 
AnswerRe: How to change a textbox text while the code is running Pin
BillWoodruff13-Sep-15 22:04
professionalBillWoodruff13-Sep-15 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.