New Posts New Posts RSS Feed: Data Reflection
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Data Reflection

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: Data Reflection
    Posted: 24-Jan-2014 at 3:36pm
I have a situation where i have an called job which is an instance of the class JobHistory. I need to pass a job object to a method called BuildNewJobFromExistingJob(). In the method, I need to create a new job object. Then i need to copy all the data from the old object to the new object. Finally i need to change a few of the fields in the object.

I am thinking of the following as a solution.

var job = Repository.CreateJobHistory()

then i want to loop through all the fields of the old object to the new object. I don't want to hard code the copying of the data as fields may be added in the future.

How do i loop through all the fields in and object copying the data from one to a new object of the same type?

Bill


Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 27-Jan-2014 at 1:54pm
Hi Bill,

One way to do this is through the use of the entity metadata. 

Below is a code snippet:

//Grab an entity metadata using its type
var entityMetadata = entityManager.MetadataStore.GetEntityMetadata(typeof(YourEntity));

//Loop through the data properties of the entity
foreach (var dataProperty in entityMetadata.DataProperties) {
//Cloning logic here
        dataProperty.SetValue(...);
}

Hope this helps.
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 27-Jan-2014 at 5:12pm
so this is what i have so far then

           var query = _manager.JobHistories
                .Where(j => j.RefNo == refNo);
           var oldJob = query.Execute().FirstOrDefault();
 
           var newJob = _manager.CreateEntity<JobHistory>();
 
           var entityMetadata = _manager.MetadataStore.GetEntityMetadata(typeof(JobHistory));
 
            foreach (var dataProperty in entityMetadata.DataProperties)
            {
                
            }

so now i need to copy the property values from oldJob to newJob

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 28-Jan-2014 at 6:12pm
That's correct. You can use the following logic to copy values from oldJob to newJob

foreach(....)
{
var valueToCopy = dataProperty.GetValue(oldJob);
dataProperty.SetValue(newJob, valueToCopy);
}
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down