Cook Computing

Testing if Method has params Parameter

October 19, 2005 Written by Charles Cook

XML-RPC.NET does not support methods with a variable number of parameters. I'm currently adding support for this so that a method can be defined like this:

interface IFoo 
  int Foo(params int[] args);

This results in the following metadata for the method:

.method public hidebysig newslot abstract virtual 
        instance int32  Foo(int32[] parms) cil managed
  .custom instance void [CookComputing.XmlRpc]CookComputing.
XmlRpc.XmlRpcMethodAttribute::.ctor() = ( 01 00 00 00 ) 
  .param [1]
  .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() 
    = ( 01 00 00 00 ) 
} // end of method IFoo::Foo

So I need to be able to determine if ParamArrayAttribute has been defined for a method parameter. I can achieve this using code like this:

Type type = typeof(IFoo);
MethodInfo mi = type.GetMethod("Foo");
ParameterInfo pi = mi.GetParameters()[0];
bool isParams = Attribute.IsDefined(pi, typeof(ParamArrayAttribute));

When serializing the method parameters into an XML-RPC request, if the ParamArrayAttribute is defined on an array parameter I output each member of the array as a separate <param> element instead of a single <param> element containing an array.