NBartomeli
Programmer
I have a function which takes 2 objects, they are both of the same class but one is from a webservice and one from the local application. Using reflection I have a method that fills the local object with the parameters in the one returned from the webservice.
In 2003 this works just fine, In 2005 it doesn't. Any ideas?
.GetFields() for the webservice returns a 0 array
.GetFields() for the local object returns a full array.
The only thing I noticed was that the local one has a ValueType as its base and the WebService object has an ObjectType as its base.
Did something change from 2003 - 2005? Thanks for any help. Here is the full function
In 2003 this works just fine, In 2005 it doesn't. Any ideas?
.GetFields() for the webservice returns a 0 array
.GetFields() for the local object returns a full array.
The only thing I noticed was that the local one has a ValueType as its base and the WebService object has an ObjectType as its base.
Did something change from 2003 - 2005? Thanks for any help. Here is the full function
Code:
/// <summary>
/// TransferObject transfers the contents of one object, transferFrom, to the receiving object, transferTo.
/// It recursively assignes complex property values.
/// </summary>
/// <param name="transferFrom">Object to transfer data from.</param>
/// <param name="transferTo">Object to transfer data to.</param>
/// <returns>Returns an object filled with the values from transferFrom and of type transferTo</returns>
public static object TransferObject(object transferFrom, object transferTo )
{
//
// TODO: Add constructor logic here
//
System.Type fromType, toType;
FieldInfo[] fromFieldInfo;
FieldInfo[] toFieldInfo;
//Get the type of the objects
fromType = transferFrom.GetType();
toType = transferTo.GetType();
//Get the fields in each type
fromFieldInfo = fromType.GetFields();
toFieldInfo = toType.GetFields();
//Loop through the fields in the transferFrom object and assign it's values to transferTo
for (int i = 0; i < fromFieldInfo.Length; i++)
{
try
{
if (fromFieldInfo[i].Name == toFieldInfo[i].Name)
//the names of the fields are the same
{
if (fromFieldInfo[i].FieldType.Namespace != "System")
//the field type is not a system type so it must be a class or structure
{
object toObject = toFieldInfo[i].GetValue(transferTo);
object fromObject = fromFieldInfo[i].GetValue(transferFrom);
if (fromObject == null)
//if the from object is null then no need to create the to object
{
toFieldInfo[i].SetValue(transferTo, null);
}
else
{
if (toObject == null)
{
//In order to load the type of the field, we need to get a refernce to the assembly
System.Reflection.Assembly asm = toFieldInfo[i].FieldType.Assembly;
//Load the type of the field
System.Type toObjectType = asm.GetType(toFieldInfo[i].FieldType.FullName);
if (toObjectType == null)
{
//It must not have been able to load the type, so just set the value to null
toFieldInfo[i].SetValue(transferTo, null);
}
else
{
//Need to create an instance of the object in order to assign property values
toObject = Activator.CreateInstance(toObjectType);
//Now since it's an object, we need to recursively set the properties of the new object before assigning it to this field
}
}
toFieldInfo[i].SetValue(transferTo, TransferObject(fromObject, toObject));
}
}
else
{
//must be a simple type so just set the value
toFieldInfo[i].SetValue(transferTo, fromFieldInfo[i].GetValue(transferFrom));
}
}
else
{
//Return an error because the objects aren't the same
throw new System.Exception("Unable to transfer object data because objects do not have the same structure");
}
}
catch (System.Exception e)
{
//catch all unhandled errors
WriteEventLog(i + ":" + e.Message);
return null;
}
}
return transferTo;
}