private static object InvokeHelper(Type type, object target, string memberName, object[] parameters)
{
BindingFlags flags = target == null ? StaticFlags : InstanceFlags;
var method = type.GetMethod(memberName, flags);
if (method == null)
{
var property = type.GetProperty(memberName, flags);
if (property == null)
{
var field = type.GetField(memberName, flags);
if (parameters.Length == 0)
{
// call the getter
return field.GetValue(target);
}
else if (parameters.Length == 1)
{
// call the setter
field.SetValue(target, parameters[0]);
return null;
}
else
{
throw new Exception("You can only call a field with 0 or 1 parameters.");
}
}
else
{
if (parameters.Length == 0)
{
// call the getter
return property.GetValue(target, null);
}
else if (parameters.Length == 1)
{
// call the setter
property.SetValue(target, parameters[0], null);
return null;
}
else
{
throw new Exception("You can only call a property with 0 or 1 parameters.");
}
}
}
else
{
return method.Invoke(target, parameters);
}
}
}