Why we got exception when number of properties is big?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, friends,

The following function works fine if the classSource has relatively smaller
number of properties. However, it would give an exception when its number of
properties gets bigger, say about 100.

Any ideas?
If this does not work, then, what is the better way to copy data from one
class to another one (they have part of properties the same)?

Thanks a lot !

------------------------------
The exception is:

Exception has been thrown by the target of an invocation.
Source: mscorlib
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[]
arguments, SignatureStruct& sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[]
arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo
culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at Utility.CopyClassData(Object classSource, Object classTarget) in
C:\UtilityFactory\Utility.cs:line 431


And here are the source code.


internal static void CopyClassData(object classSource, object
classTarget)
{
Type typeInfoSource = classSource.GetType();
PropertyInfo[] propertiesSource = typeInfoSource.GetProperties();

Type typeInfoTarget = classTarget.GetType();
PropertyInfo[] propertiesTarget = typeInfoTarget.GetProperties();

foreach (PropertyInfo propertySource in propertiesSource)
{
foreach (PropertyInfo propertyTarget in propertiesTarget)
{
if (String.Compare(propertySource.Name,
propertyTarget.Name, true) == 0)
{
System.Object propertyValue =
propertySource.GetValue(classSource, null);
propertyTarget.SetValue(classTarget, propertyValue,
null);
}
}
}
}
 
I think there is a

IsWriteable

or

IsReadonly

property as well, make sure you check that before using the GetValue,
SetValue thing.

I know there is some property to check, I just can't remember the name
offhand.
 
Back
Top