Edit: root cause of the question
I'm working on an application, that uses System.Messaging
and XML serialisation via XmlMessageFormatter
.
I would like to send an object, let's say Class1
, having an ID field:
public class Class1{
public long Id1;
}
I would also like to send another object, let's say Class16
, having another ID field:
public class Class16{
public long Id16;
}
In XML, both need to look like:
<HM>Human_Message
<ID>Own_Identifier</ID>
</HM>
In order to achieve this, I'm working with following [Xml]
-like configurations:
Class1:
[XmlRoot(ElementName = "HM")]
public class Class1{
[XmlElement(ElementName = "ID")]
public long Id1;
}
Class16:
[XmlRoot(ElementName = "HM")]
public class Class16{
[XmlElement(ElementName = "ID")]
public long Id16;
}
As you see, the XML body will indeed be equal for both classes.
Is this even possible?
Edit: original question
I have a basic class (simple class), from which there are several subclasses (about 27 of them), inheriting from it.
I'm using standard C# System.Messaging
system for sending objects back and forth.
Very simplified:
Sending side:
I have a MessageQueue, doing:
subClass1 Obj1 = subClass1(...);
...
Basic_Class Obj_To_Be_Sent = Basic_Class(Obj1);
System.Messaging.Message message = new System.Messaging.Message(Obj_To_Be_Sent);
obj_MessageQueue.Send(message, ...);
When checking Obj_To_Be_Sent
, the type is correct.
Once this is sent, when I have a look at Computer Management, Services and Applications, Message Queuing, ..., Properties, I see the message, but I can't verify if the type is still correct.
Receiving side:
I have an _xmlMessageFormatter
, containing (amongst others):
System.Type[] messageTypes = new System.Type[27];
messageTypes[0] = typeof(SubClass1);
...
messageTypes[15] = typeof(SubClass16);
...
message = this._receiveQueue.Receive();
Basic_Class general = (Basic_Class)this._xmlMessageFormatter.Read(message);
Type objectType= general.GetType();
To my surprise, objectType
is wrong (it is believed to be SubClass16
).
This application has worked fine before, but now something seems to fail. The biggest problem I have is that I don't know how to check the steps between sending the message and getting the type of the received message.
Does anybody have knowledge on Computer Management, Services and Applications, Message Queuing, ..., how can I check if the object type on the sending side is ok?
Does anybody have knowledge on _xmlFormatter.Read()
and GetType()
? (Already after the Read()
, the watch-window mentions the Type of general
to be wrong)
Thanks in advance
Edit after more investigation
I delete my own answer as the problem is not completely solved.
Meanwhile I've discovered that [XmlRoot]
entries are causing the mentioned issues: I've been using the same [XmlRoot]
entry for different classes.
Is there a way to differentiate?
For your information, I've already tried the following but it did not work:
Class1:
[XmlRoot(ElementName = "HM", DataType = "subClass1", Namespace="Namespace")]
public class subClass1 : Basic_Class
Class2:
[XmlRoot(ElementName = "HM", DataType = "subClass16", Namespace="Namespace")]
public class subClass16 : Basic_Class
while the _xmlFormatter.TargetTypes
contained entries like:
Name = "subClass1" FullName="Namespace.Class1"
Name = "subClass16" FullName="Namespace.Class16"
Does anybody have any ideas?