Click here to Skip to main content
15,888,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Generic class:

C#
public class GenericEntityMapping
{
    public T Map<t>(XmlNode xNode, T entityObject)
    {
        //FYI.
        //PropertyInfo[] entityProperties = typeof(T).GetProperties();
        //foreach (PropertyInfo pInfo in entityProperties){...}

        //somewhere in the code depends on the condition.
        if true then...
        GenericEntityMapping<SampleEntity01>.Map(node, new SampleEntity01())
        .
        .
        .
        else if true then...
        GenericEntityMapping<SampleEntity02>.Map(node, new SampleEntity02())
    }
}



How can i do it where the type in the generic needs not to be hardcoded?


My goal is something like:
Type myType = pInfo.PropertyType.GetType();
GenericEntityMapping<myType>.Map(node, System.Activator.CreateInstance<myType>());

Is it possible?
Posted
Updated 1-Nov-11 18:56pm
v6
Comments
Sergey Alexandrovich Kryukov 1-Nov-11 23:19pm    
How come you have a problem with that in principle? Any particular problem?
--SA
Jephunneh Malazarte 2-Nov-11 1:00am    
Hello SA - the problem arise when mapping the xmlnode to the entities where the number of xml nodes are not constant. example: <MYPARENTNODE><MYCHILD><NAME>my name</NAME> <AGE>12</AGE> </MYCHILD><OTHERCHILD>... </OTHERCHILD>. . . </MYPARENTNODE>however there are cases that it has only one child node.. so the requirement is to mapped the nodes to the entity and i was thinking that maybe it would be less coding modification if i do it dynamically.
Jephunneh Malazarte 2-Nov-11 1:59am    
May I ask, if the requirement is to mapped the xmlnodes to the entity. how will you do it? Thanks in advance. My code is not finally though and it's good to have variety of solutions... hope you don't mind me asking SA. :)
BillWoodruff 2-Nov-11 0:41am    
+5 for this interesting question, thanks ! Am I correct in assuming that this question is asked in the context of using nHibernate or EF ?
Jephunneh Malazarte 2-Nov-11 1:04am    
hello Bill - somewhat related to EF :) thanks.

For a type, being generic and begin static are not related. Both generic and non-generic types can be static or not static.

—SA
 
Share this answer
 
Comments
Jephunneh Malazarte 2-Nov-11 0:11am    
thanks for your reply and yes you are right sorry my bad. I use a wrong terminology instead of hardcoded i use static. Sorry about that.
Sergey Alexandrovich Kryukov 2-Nov-11 1:07am    
Unfortunately, you still use hard-coded "Map". I the type GenericEntityMapping was mine, I would easily avoid it. I would make it implementing an interface with the only method Map and would find MethodInfo through this interface.

Now, is GenericEntityMapping your type? If so, you could greatly improve your approach.

--SA
Jephunneh Malazarte 2-Nov-11 1:42am    
oh no.. the Map is the generic function that i need to call recursively the GenericEntityMapping is my generic class where the Map function is located. I am curious which part of the code that I need to improve. Any inputs are appreciated. FYI. The EntityType are the one that I need to be dynamic.

-- New --
thanks for the add-on i will use interface to prevent any hardcoded value :) thanks SA.

thanks in advance SA.
Sergey Alexandrovich Kryukov 2-Nov-11 21:48pm    
Maybe you can explain more on the ultimate goals of this activity. What Map does What entity mapping does? Why there are several SampleEntities in the pseudo-code? Do you want to get rid of each of them?
--SA
Jephunneh Malazarte 3-Nov-11 8:02am    
Business Requirements:
There is a separate website that manages all application. Lets name it as "One Website", then it manages 2 system the System A and System B. The one website will generate the dynamic fields depending on which system the user selected. These dynamically generated fields will be stored into XML file along with other related/unrelated information.

Now in order for the System A to processs the request triggered by the "one website" the System A needs to expose a web service (i am using restful approach here). One website will call the service of System A and pass on the XML.

Question:
What approach can you suggest in order to save the selected data from XML to my database tables?

What I did here:
Is that I have database tables and i have created an entity class that contains the db tables columns. Then crated a function to map the xml nodes to my entity objects.

I have posted my solution here: http://jephunnehrm.xanga.com/756318399/mapping-of-xml-node-to-entity-objects/
feel free to add suggestion for improvement...

Really appreciate your response and sorry for the hassle :p
Hello SAKryukov,
Thanks for the quick reply.

I found the solution:

C#
MethodInfo method = typeof(GenericEntityMapping).GetMethod("Map");
MethodInfo generic = method.MakeGenericMethod(Type.GetType(pInfo.PropertyType.FullName));

object[] objParam = { node, System.Activator.CreateInstance(Type.GetType(pInfo.PropertyType.FullName)) };
pInfo.SetValue(entity, generic.Invoke(this, objParam), null);
 
Share this answer
 
Comments
BillWoodruff 2-Nov-11 0:41am    
+5 for an interesting answer, thanks. I wish I could figure out exactly what this code is doing :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900