Hi,
I posted a question in C# forum and a guy there advice me to ask the question here.
So, here is a link to my question:
http://www.daniweb.com/forums/thread190093.html
First, I will describe the classes and then I will describe the error:
I have a class Task3:
public class Task3
{
private long m_TaskId;
private string m_CustomerCompanyNumber;
private GroupType m_GroupType; //GroupType is Enum
private DateTime m_StartDate;
//CustomFieldData soposed to be abstract and is has 4 sons class - this field is the problem.
private List<CustomFieldData> m_CustomFieldsData;
//TaskContainer is a class with 4 string members - I will not add this class for simplicity.
private TaskContainer m_TaskContainer = null;
[XmlElement(DataType = "long")]
public long TaskId
{
get { return m_TaskId; }
set { m_TaskId = value; }
}
[XmlElement(DataType = "string")]
public string GroupTypeName
{
get { return Enum.GetName(typeof(GroupType), m_GroupType); }
set
{
if (Enum.IsDefined(typeof(GroupType), value))
{
m_GroupType = (GroupType)Enum.Parse(typeof(GroupType), value);
}
}
}
public GroupType GroupType
{
get { return m_GroupType; }
}
[XmlElement(DataType = "string")]
public string CustomerCompanyNumber
{
get { return m_CustomerCompanyNumber; }
set { m_CustomerCompanyNumber = value; }
}
[XmlElement(Type = typeof(DateTime))]
public DateTime StartDate
{
get { return m_StartDate; }
set { m_StartDate = value; }
}
[XmlArray(ElementName="CustomFields")]
public CustomFieldData[] CustomFieldsData
{
get { return m_CustomFieldsData.ToArray(); }
set
{
m_CustomFieldsData = new List<CustomFieldData>(value);
}
}
[XmlElement(Type = typeof(TaskContainer))]
public TaskContainer TaskContainer
{
get { return m_TaskContainer; }
set { m_TaskContainer = value; }
}
//C'tors+other methods
} public class Task3
{
private long m_TaskId;
private string m_CustomerCompanyNumber;
private GroupType m_GroupType; //GroupType is Enum
private DateTime m_StartDate;
//CustomFieldData soposed to be abstract and is has 4 sons class - this field is the problem.
private List<CustomFieldData> m_CustomFieldsData;
//TaskContainer is a class with 4 string members - I will not add this class for simplicity.
private TaskContainer m_TaskContainer = null;
[XmlElement(DataType = "long")]
public long TaskId
{
get { return m_TaskId; }
set { m_TaskId = value; }
}
[XmlElement(DataType = "string")]
public string GroupTypeName
{
get { return Enum.GetName(typeof(GroupType), m_GroupType); }
set
{
if (Enum.IsDefined(typeof(GroupType), value))
{
m_GroupType = (GroupType)Enum.Parse(typeof(GroupType), value);
}
}
}
public GroupType GroupType
{
get { return m_GroupType; }
}
[XmlElement(DataType = "string")]
public string CustomerCompanyNumber
{
get { return m_CustomerCompanyNumber; }
set { m_CustomerCompanyNumber = value; }
}
[XmlElement(Type = typeof(DateTime))]
public DateTime StartDate
{
get { return m_StartDate; }
set { m_StartDate = value; }
}
[XmlArray(ElementName="CustomFields")]
public CustomFieldData[] CustomFieldsData
{
get { return m_CustomFieldsData.ToArray(); }
set
{
m_CustomFieldsData = new List<CustomFieldData>(value);
}
}
[XmlElement(Type = typeof(TaskContainer))]
public TaskContainer TaskContainer
{
get { return m_TaskContainer; }
set { m_TaskContainer = value; }
}
//C'tors+other methods
}
Class CustomFieldData soposed to be abstract but I cannot make it abstract - it throws an exception when deserialize happened:
public class CustomFieldData
{
private CustomFieldType m_CustomFieldType;
[XmlElement(DataType = "string")]
public string Type
{
get { return Enum.GetName(typeof(CustomFieldType), m_CustomFieldType); }
set
{
if (Enum.IsDefined(typeof(CustomFieldType), value))
{
m_CustomFieldType = (CustomFieldType)Enum.Parse(typeof(CustomFieldType), value);
}
}
}
//public abstract long CustomFieldId { get; set; }
//public abstract string CustomFieldName { get; set; }
//public abstract string Value { get; set; }
public CustomFieldData() { }
public CustomFieldData(CustomFieldType type)
{
m_CustomFieldType = type;
}
} public class CustomFieldData
{
private CustomFieldType m_CustomFieldType;
[XmlElement(DataType = "string")]
public string Type
{
get { return Enum.GetName(typeof(CustomFieldType), m_CustomFieldType); }
set
{
if (Enum.IsDefined(typeof(CustomFieldType), value))
{
m_CustomFieldType = (CustomFieldType)Enum.Parse(typeof(CustomFieldType), value);
}
}
}
//public abstract long CustomFieldId { get; set; }
//public abstract string CustomFieldName { get; set; }
//public abstract string Value { get; set; }
public CustomFieldData() { }
public CustomFieldData(CustomFieldType type)
{
m_CustomFieldType = type;
}
}
I have 4 classes that inherits CustomFieldData but I will show only 2 of them because it is enough info:
class FieldData : CustomFieldData
{
private long m_FieldId;
private string m_FieldName;
private string m_Value;
[XmlElement(DataType = "long")]
public long CustomFieldId //Soposed to be override property
{
get { return m_FieldId; }
set { m_FieldId = value; }
}
[XmlElement(DataType = "string")]
public string CustomFieldName //Soposed to be property method
{
get { return m_FieldName; }
set { m_FieldName = value; }
}
[XmlElement(DataType = "string")]
public string Value //Soposed to be override property
{
get { return m_Value; }
set { m_Value = value; }
}
public FieldData() { }
public FieldData(long id, string name, string value)
: base(CustomFieldType.CustomField)
{
m_FieldId = id;
m_FieldName = name;
m_Value = value;
}
}
class ListData : CustomFieldData
{
private long m_ListInGroupId;
private string m_ListName;
private string m_Value;
private long m_ValueId;
[XmlElement(DataType = "long")]
public long CustomFieldId //Soposed to be override property
{
get { return m_ListInGroupId; }
set { m_ListInGroupId = value; }
}
[XmlElement(DataType = "string")]
public string CustomFieldName //Soposed to be override property
{
get { return m_ListName; }
set { m_ListName = value; }
}
[XmlElement(DataType = "string")]
public string Value //Soposed to be override property
{
get { return m_Value; }
set { m_Value = value; }
}
public long ValueId
{
get { return m_ValueId; }
set { m_ValueId = value; }
}
public ListData() { }
public ListData(long id, string name, long valueId)
: base(CustomFieldType.CustomList)
{
m_ListInGroupId = id;
m_ListName = name;
m_ValueId = valueId;
m_Value = CustomGroupsDBHandler.GetListItem(m_ListInGroupId, valueId);
}
public ListData(long id, string name, long valueId, string value)
: base(CustomFieldType.CustomList)
{
m_ListInGroupId = id;
m_ListName = name;
m_ValueId = valueId;
m_Value = value;
}
} class FieldData : CustomFieldData
{
private long m_FieldId;
private string m_FieldName;
private string m_Value;
[XmlElement(DataType = "long")]
public long CustomFieldId //Soposed to be override property
{
get { return m_FieldId; }
set { m_FieldId = value; }
}
[XmlElement(DataType = "string")]
public string CustomFieldName //Soposed to be property method
{
get { return m_FieldName; }
set { m_FieldName = value; }
}
[XmlElement(DataType = "string")]
public string Value //Soposed to be override property
{
get { return m_Value; }
set { m_Value = value; }
}
public FieldData() { }
public FieldData(long id, string name, string value)
: base(CustomFieldType.CustomField)
{
m_FieldId = id;
m_FieldName = name;
m_Value = value;
}
}
class ListData : CustomFieldData
{
private long m_ListInGroupId;
private string m_ListName;
private string m_Value;
private long m_ValueId;
[XmlElement(DataType = "long")]
public long CustomFieldId //Soposed to be override property
{
get { return m_ListInGroupId; }
set { m_ListInGroupId = value; }
}
[XmlElement(DataType = "string")]
public string CustomFieldName //Soposed to be override property
{
get { return m_ListName; }
set { m_ListName = value; }
}
[XmlElement(DataType = "string")]
public string Value //Soposed to be override property
{
get { return m_Value; }
set { m_Value = value; }
}
public long ValueId
{
get { return m_ValueId; }
set { m_ValueId = value; }
}
public ListData() { }
public ListData(long id, string name, long valueId)
: base(CustomFieldType.CustomList)
{
m_ListInGroupId = id;
m_ListName = name;
m_ValueId = valueId;
m_Value = CustomGroupsDBHandler.GetListItem(m_ListInGroupId, valueId);
}
public ListData(long id, string name, long valueId, string value)
: base(CustomFieldType.CustomList)
{
m_ListInGroupId = id;
m_ListName = name;
m_ValueId = valueId;
m_Value = value;
}
}
I have also class Movement that inherit class Task. Movement and Task are deserialized ok except of the CustomFieldsData array member that all of its items are CustomFieldData and therefore doesn't contains the CustomListData members (ValueId, Value, CustomFieldName, CustomFieldId for example).
What I am doing is the following:
1. From client code (javascript) I call a webservice that returns Movement. Here the data serialized great and I have all the data I need with the right properties.
2. The user make some operations and clicks on the "approve" button. The updated data sent back to the server (the user didn't touch and didn't change the CustomFieldData array data).
3. Data sent back to the server and deserialized automatically using the asp.net webservice mechanizem (I think).
4. The data didn't serialized weel and instead of getting the correct data in the CustomFieldData array, I get an array with CustomFieldData items. For example:
CustomFieldData contains: {FieldData, FieldData, ListData}
after data sent back to the server, CustomFieldData contains {CustomFieldData,CustomFieldData,CustomFieldData} and therefore I am loosing information (I don't have the ValueId member and casting throws an exception). The deserialization creates CustomFieldData objects and not what I really need. Where in fact, at the beginning, I needed the class CustomFieldData to be abstract!
How Can I get the proper data?
Thank you very much!