public class ReflectionParameters : ParameterData {
ParameterInfo [] pi;
Type [] types;
- bool last_arg_is_params = false;
- bool is_varargs = false;
+ int params_idx = -1;
+ bool is_varargs;
ParameterData gpd;
public ReflectionParameters (MethodBase mb)
{
- object [] attrs;
-
ParameterInfo [] pi = mb.GetParameters ();
is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
this.pi = pi;
- int count = pi.Length-1;
+ int count = pi.Length;
- if (pi.Length == 0) {
+ if (count == 0) {
types = Type.EmptyTypes;
- } else {
- types = new Type [pi.Length];
- for (int i = 0; i < pi.Length; i++)
- types [i] = pi [i].ParameterType;
+ return;
}
- if (count < 0)
- return;
+ types = new Type [count];
+ for (int i = 0; i < count; i++)
+ types [i] = pi [i].ParameterType;
+ // TODO: This (if) should be done one level higher to correctly use
+ // out caching facilities.
MethodBase generic = TypeManager.DropGenericMethodArguments (mb);
if (generic != mb) {
gpd = TypeManager.GetParameterData (generic);
- last_arg_is_params = gpd.HasParams;
+ if (gpd.HasParams) {
+ for (int i = gpd.Count; i != 0; --i) {
+ if ((gpd.ParameterModifier (i-1) & Parameter.Modifier.PARAMS) != 0) {
+ this.params_idx = i-1;
+ break;
+ }
+ }
+ }
return;
}
- attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
- if (attrs == null)
- return;
+ //
+ // So far, the params attribute can be used in C# for the last
+ // and next to last method parameters.
+ // If some other language can place it anywhere we will
+ // have to analyze all parameters and not just last 2.
+ //
+ --count;
+ for (int i = count; i >= 0 && i > count - 2; --i) {
+ if (!pi [i].ParameterType.IsArray)
+ continue;
+
+ object [] attrs = pi [i].GetCustomAttributes (TypeManager.param_array_type, true);
+ if (attrs.Length == 1) {
+ params_idx = i;
+ return;
+ }
+ }
+ }
- if (attrs.Length == 0)
- return;
+ public override bool Equals (object obj)
+ {
+ ReflectionParameters rp = obj as ReflectionParameters;
+ if (rp == null)
+ return false;
+
+ if (Count != rp.Count)
+ return false;
- last_arg_is_params = true;
+ for (int i = 0; i < Count; ++i) {
+ if (!types [i].Equals (rp.types [i]))
+ return false;
+ }
+ return true;
+ }
+
+ public override int GetHashCode ()
+ {
+ return base.GetHashCode ();
}
public string GetSignatureForError ()
public Type ParameterType (int pos)
{
- if (last_arg_is_params && pos >= pi.Length - 1)
- return pi [pi.Length - 1].ParameterType;
- else if (is_varargs && pos >= pi.Length)
+ if (is_varargs && pos >= pi.Length)
return TypeManager.runtime_argument_handle_type;
- else {
- Type t = pi [pos].ParameterType;
- return t;
- }
+ return pi [pos].ParameterType;
}
public string ParameterName (int pos)
if (gpd != null)
return gpd.ParameterName (pos);
- if (last_arg_is_params && pos >= pi.Length - 1)
- return pi [pi.Length - 1].Name;
- else if (is_varargs && pos >= pi.Length)
+ if (is_varargs && pos >= pi.Length)
return "__arglist";
- else
- return pi [pos].Name;
+
+ return pi [pos].Name;
}
public string ParameterDesc (int pos)
sb.Append ("ref ");
}
- if (pos >= pi.Length - 1 && last_arg_is_params)
+ if (params_idx == pos)
sb.Append ("params ");
sb.Append (TypeManager.CSharpName (partype).Replace ("&", ""));
public Parameter.Modifier ParameterModifier (int pos)
{
- if (last_arg_is_params && pos >= pi.Length - 1)
+ if (pos == params_idx)
return Parameter.Modifier.PARAMS;
else if (is_varargs && pos >= pi.Length)
return Parameter.Modifier.ARGLIST;
}
public bool HasParams {
- get { return last_arg_is_params; }
+ get { return params_idx != -1; }
}
public Type[] Types {