Just want to stress out this question is irrelevant of why this technology and why not that. This is related to C# code.
I searched below but none provides solution.
Serialize a class that implements IEnumerable
Serialization on classes that implement IEnumerator
I am working on WCF Rest project in C# which is replacing .NET Remoting communication layer. I use Newtonsoft dll to serialize and de-serialize.
I have a class called MyDTO
which implements IEnumerator
, IEnumerable
which I cannot change since this is old class and many applications in production use them. When I try to serialize MyDTO
into string I do not get any error/exception message, I just get an empty array like "[]". Can anybody tell how we can serialize/deserialze class that implements IEnumerator
, IEnumerable
?
public class MyDTO : IEnumerator, IEnumerable
I am calling a method called ABC in OldServer.dll which gives me MyDTO object. I want to convert this to string and again from string to MyDTO.
Please let me know if you need more information. Please see below for MyDTO
class which I cannot change:
[Serializable]
public class MyDTO : IEnumerator, IEnumerable
{
#region Inner Types
internal class MyObjComparer : IComparer<MyObj>
{
public int Compare(MyObj x, MyObj y)
{
return x.InputCode.CompareTo(y.InputCode);
}
}
#endregion
#region Variables
private List<MyObj> myObjList;
private string selectedObjectMessage;
private bool containSequenceNo = false;
private bool sortEnabled;
private bool filterEnabled;
private IComparer<MyObj> objectComparer;
#endregion
#region Constructors
public MyDTO()
{
this.myObjList = new List<MyObj>();
this.selectedObjectMessage = string.Empty;
}
public MyDTO(List<MyObj> objects)
{
this.myObjList = objects;
this.selectedObjectMessage = string.Empty;
}
public MyDTO(IComparer<MyObj> argSortComparer)
: this()
{
this.objectComparer = argSortComparer;
}
public MyDTO(List<MyObj> argErrors, IComparer<MyObj> argSortComparer)
: this(argErrors)
{
this.objectComparer = argSortComparer;
}
public MyDTO(List<MyObj> argErrors, IComparer<MyObj> argSortComparer, bool argSortEnabled)
: this(argErrors, argSortComparer)
{
this.sortEnabled = argSortEnabled;
}
#endregion
#region Properties
public string selectedObjectMessage
{
get { return this.selectedObjectMessage; }
set
{
if (value == null)
value = string.Empty;
this.selectedObjectMessage = value;
}
}
public int Count
{
get { return this.myObjList.Count; }
}
public bool ErrorsContainsSequenceNo
{
get { return this.containSequenceNo; }
set { this.containSequenceNo = value; }
}
public List<MyObj> myObjList
{
get { return this.myObjList; }
set { this.myObjList = value; }
}
public MyDTO WithoutEmptyMyObjects
{
get
{
MyDTO objs = new MyDTO();
foreach (MyObj obj in this.myObjList)
{
if (!string.IsNullOrEmpty(obj.InputCode))
objs.Add(obj);
}
return objs;
}
}
#endregion
}
UPDATE 1:
After spending almost a day we decided to write our own method targeting MyDTO which will produce XmlDocument which is serialize. Using that same XmlDocument we will try create MyDTO object. I feel we need to just concentrate on properties that has setters. I will let you know.