/ Published in: C#
Say that you have a collection of objects empty but for IDs.
You want to do a foreach loop through those objects and lookup detail one at a time.
Unfortunately, in the foreach, you can't replace the references, you can only copy the new detail into the object.
This technique will populate your current reference from an object instance returned by a lookup.
You want to do a foreach loop through those objects and lookup detail one at a time.
Unfortunately, in the foreach, you can't replace the references, you can only copy the new detail into the object.
This technique will populate your current reference from an object instance returned by a lookup.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Lookup result of a higher level object which has a dogs list. result.Dogs.ForEach(d => PopulateDogObject(d)); //Helper public void PopulateDogObject(IDog toDog) { IDog lookupResult = GetDogFromWebService(toDog.DogID); lookupResult.PopulateIDog(toDog); } //This is on the dog class. public void PopulateIDog(IDog toDog) { foreach (PropertyInfo pi in fromFields) { pi.SetValue(toDog, pi.GetValue(this, null), null); } }