Home All Groups Group Topic Archive Search About

Why we got exception when number of properties is big?

Author
22 Oct 2007 7:51 PM
Andrew
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);
                    }
                }
            }
        }

Author
22 Oct 2007 9:28 PM
sloan
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.





Show quote
"Andrew" <And***@discussions.microsoft.com> wrote in message
news:B15D16B0-938B-435C-B381-48FAA0C9E0C4@microsoft.com...
> 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);
>                    }
>                }
>            }
>        }
>
Author
23 Oct 2007 6:34 PM
nobugz
Check the InnerException property of the exception for the real error.

AddThis Social Bookmark Button