Database Reference
In-Depth Information
C# reflection
Reflection cannot be underestimated, especially when working with XML serialization and
Entity Framework.
C# uses reflection mostly with the System.Reflection namespace; for further reading
see http://msdn.microsoft.com/en-us/library/system.reflection%28v=vs.110%29.aspx . Re-
flection can be used to get information from assemblies, http://msdn.microsoft.com/en-us/
library/ms173183.aspx , or to get information from object and classes, ht-
tp://msdn.microsoft.com/en-us/library/b05d559ty%28v=vs.110%29.aspx .
So why does this help with all this development? Being able to walk through an object and
copy fields to fields, as with a deep cloning of an object, can only be done with reflection.
When copying from a message to a database row, it does not necessarily have to be a one-
to-one copy, but it saves having to know all the fields of an object or changing the code
when the fields of the objects change. Here's a snippet from the PaymentDAL.cs file that
shows a copy of the address piece of the message object being copied into the Payments
table address value with a payment row of data. In this piece of code, we are copying val-
ues from a one object to a different object with matching field names and putting in values,
without calling these fields directly, which would involve a lot more code and work.
/*****
* Copy the values of the old Address object
* to the address fields in the database
* *******/
Address address =
(Address)payPropertyInformation.GetValue(paymentReq, null);
PropertyInfo[]
addressProperty = address.GetType().GetProperties();
// Get each field from the address object
for (int index4 = 0; index4 < addressProperty.Length;
index4++)
{
PropertyInfo addressPropertyInformation =
addressProperty[index4];
string addressName =
addressPropertyInformation.Name.ToString();
// Get the address field value
Search WWH ::




Custom Search