Csla Reflection Code

Csla Reflection Code

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


razorkai posted on Wednesday, July 12, 2006

Hi guys.

I'm trying to call a static method dynamically and found this code in the Csla MethodCaller class.

    public static object CallMethod(
      object obj, MethodInfo info, params object[] parameters)
    {
      // call a private method on the object
      object result;
      try
      {
        result = info.Invoke(obj, parameters);
      }
      catch (Exception e)
      {
        throw new Csla.Server.CallMethodException(
          info.Name + " " + Resources.MethodCallFailed, e.InnerException);
      }
      return result;
    }

What caught my attention is the parms object[] part as I had not seen it before.  The method I am trying to invoke is actually a Csla Factory method on one of my business objects.  I have all the parameters in a criteria object and so I wrote a method, using the example above, like this:-

public object CallMethod(MethodInfo info, params object[] parameters)
{
      return info.Invoke(null, parameters);
}

The static method I am trying to invoke takes one parameter, a Guid.  If I step into this code I can see I have a valid MethodInfo for the method, and the parameters parameter is correctly identified as MyObject.Criteria.  I can drill down into that and see that the array is one dimension deep and contains a Guid.  However, the code spits out an error that it cannot convert an object of type MyObject.Criteria to a Guid.  The Csla code works great, and everything I can find on this way of calling a method seems to indicate that what I have done should work.  So anyone out there know why it blows up?

TIA.

Final thought:  I just made a quick Test rig to Test this outside my project and get the same result.  Please see attached code.

dean replied on Thursday, July 13, 2006

I don't see any attached code. I want to see how you construct you info object.

Dean

razorkai replied on Friday, July 14, 2006

Weird!  I definitely attached it but it seems it has gone.  Anyway, I figured out that I wasn't thinking straight.  The Factory method I was trying to invoke doesn't take a criteria object but a parameter of type Guid.  Therefore I was never going to invoke it by passing it a Criteria object containing a Guid.  The Csla invokes DataPortal methods within the Business Objects by using this technique, but that works because they expect a Criteria object.  The Csla does not invoke the static methods this way, it is the static methods that end up invoking the DataPortal methods!  Thanks for showing an interest though!

Copyright (c) Marimer LLC