By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

XmlSerialization - Implementing dynamic "caster"?? (cast Type not know until run-time)

P: n/a
I am trying to create a generic SOA ServiceInvoker that will accept an
XML string that will be used to deserialize an object generated by
XSDObjectGen. The hierarchy goes like this:
Requests...Request (1..n)... Payload (1)... PayloadCollection (1) ...
Payload (0...n)

I have the knowledge about the root object Responses which I can create
directly. However I don't know what is the type for Request and Payload
(These will be versioned classes and its class names will be stored in
a Requests repository (a .config file)

(See code to follow)
In my ServiceInvoker.Invoke method I am receiving an input string
parameter: xmlPayload.
Strings strRequestObjClassName and strRequestPayloadObjClassName will
be retrieved from .Config file and used to create appropriate instances
via Reflection. (I hard coded these values). Remember these have to
come from .Config file.

Newly created Payload object needs be initialized via
XMLSerializer.Deserialize method. I am first appending it
payloadObjectCollection that is part of newly created Request object.
Here I start using "dynamic casting" (that is just my term I use to
explain the problem). And the problem is right there. I don't know what
the real type is when applying cast operator. It comes from the Type of
an instance created via Reflection.
Is it prohibited to use cast operation in this way. If yes, I need to
find another solution.

After (un)sucessful (!!!) deserialization this Request object should
then be added to a concrete object Requests (that I can instantiate
normally via new operator)

This is what I am trying to do:

string soaResponses ServiceInvoker.Invoke(string xmlPayload)
{
Assembly asm = Assembly.LoadFrom("InvocationManager.dll");

string strRequestObjClassName="soa.mycompany.soaRequest"
object requestObj=asm.CreateInstance(strRequestObjClassNa me,true);

string strRequestPayloadObjClassName="soa.mycompany.soaRe questPayload"
object
payloadObj=asm.CreateInstance(strRequestPayloadObj ClassName,true);

((requestObj.ReflectedType)requestObj).payload.pay loadObjectCollection.Add(payloadObj);

XmlSerializer xsPayloadObj = new
XmlSerializer(payloadObj.ReflectedType);

StringReader srPayloadObj=new StringReader(xmlPayload);

((requestObj.ReflectedType)requestObj).payload.pay loadObjectCollection[0]=
(payloadObj.ReflectedType) xsPayloadObj.Deserialize(new
XmlTextReader(srPayloadObj));

....
}

I have at least one error for now.
The type or namespace name 'requestObj' could not be found (are you
missing a using directive or an assembly reference?

Once this is resolved I would expect to have an error for
(payloadObj.ReflectedType) cast.

I tried also ((requestObj.GetType())requestObj).payload....

I must have this working. Pleeeese help.
If not this way any other way would do. It has to be dynamic.

Thanks for help
Rad

Nov 12 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.