Hi everybody.
I'm invoking public factory methods using Csla Reflection, and came across a particular case where
public static object CallFactoryMethod(Type objectType, string method, params object[] parameters)
{
object returnValue;
MethodInfo factory = objectType.GetMethod(method, factoryFlags, null, MethodCaller.GetParameterTypes(parameters), null);
if (factory == null)
....
presents an issue when it calls [MethodCaller.GetParameterTypes(parameters)] and [parameters] is null.
When [GetParameterTypes(null) is executed, it adds [result.Add(typeof(object));] to the result [type] list.
public static Type[] GetParameterTypes(object[] parameters)
{
List<Type> result = new List<Type>();
if (parameters == null)
{
result.Add(typeof(object));
}
else
I have a particular case where I define 2 public static methods on a class like this: 1. public static NewObject() {...} 2. public static NewObject(object obj} {...}
By default, when I try to Invoke the 1st. method with no parameters, CallFactoryMethod(..) with [null] as params[], it defaults to the 2nd. version of the method, due to its default parameter added by GetParameterTypes(..).
And it throws an exception because I'm not sending the [object] parameter, instead, I'm sending null as params.
I didn't get to check the implications of changing this behaviour on CSLA.
If anyone can suggest a simple workaround, it's welcome.
For now I'll use my own Reflection Functions to call on this factory methods for NewObject on my BOs.
Thanks to all,
Troncho
It has been a long time since I've looked at that code, but it is complex to distinguish between no parameter, null parameter, and param arrays (which might be null or not). I do know there are some cases we just never figured out how to solve, and if you look at the data portal flow (for example) you'll see that we pass a second flag (or special value) to indicate the total absence of a parameter vs a null or empty param array.
Thanks for your answer Rocky. You are absolutely right about it. I've just copied the code to an independant static method so I can get a parameterless constructor. The idea of an extra flag param is an excelent choice in thes cases.
Copyright (c) Marimer LLC