Utilities.CallByName method with CallType.Set fails with ArgumentNullException

Utilities.CallByName method with CallType.Set fails with ArgumentNullException

Old forum URL: forums.lhotka.net/forums/t/2040.aspx


twistedstream posted on Thursday, December 28, 2006

I'm attempting to use the CallByName method in the CSLA.NET 2.0 Utilities class to set the value of a property in a late-bound fashion:

Utilities.CallByName(myBo, CallType.Set, "PropertyName", "my value");

No matter how I invoke CallByName with CallType.Set, it always throws an ArgumentNullException.  Digging into the CSLA.NET source, I think I can see why:

case CallType.Set:
  {
    PropertyInfo p = target.GetType().GetProperty(methodName);
    object[] index = null;
    args.CopyTo(index, 1);
    p.SetValue(target, args[0], index);
    return null;
  }

The variable index is never referencing an array instance, yet is passed into the CopyTo method of the args array.  According to the documentation for Array.CopyTo, passing null for the destination array will cause this exception to be thrown.

Is this a bug?

~pete

gbahns replied on Tuesday, March 27, 2007

Pete,

I just ran into this same problem.  It appears that it's a bug in the CallByName method, but not a bug in CSLA per se since the method is never used to set property values.  You probably answered this for yourself three months, but for the benefit of others, the "index" local variable and associated "args.CopyTo" statement are not needed, and "null" can be passed as the third argument to p.SetValue.

Alternatively, I think what was originally intended was the following:

case CallType.Set:
{
PropertyInfo p = target.GetType().GetProperty(methodName);
object[] index = null;
if (args.Length > 1)
{
index =
new object[args.Length - 1];
args.CopyTo(index, 1);
}
p.SetValue(target, args[0], index);
return null;
}

I haven't tested this because I don't need it, so if you use it please test it first.

Greg

Copyright (c) Marimer LLC