Question

Well, I'm confused by the properties of the ParameterInfo class.
Unfortunately documentation is not very clear: examples show how to build methods but don't show how these methods look in C#.

Cane somebody tell more about these properties:

  • DefaultValue
  • HasDefaultValue
  • IsIn
  • IsLcid
  • IsOptional
  • IsOut
  • IsRetval

And which combination leads to what method params.
I made a simple program which gives the following output:

Method name M1 void M1(object param)
IL signature: .method public hidebysig instance void M1(object param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=False
IsRetVal=False


Method name M2 void M2(object param = null)
IL signature .method public hidebysig instance void M2([opt] object param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=True
DefaultValue=null
IsIn=False
IsLcid=False
IsOptional=True
IsOut=False
IsRetVal=False


Method name M3 void M3(out object param)
IL signature .method public hidebysig instance void M3([out] object& param) cil managed
Method parameter description:
Is passed by reference True
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=True
IsRetVal=False


Method name M4 void M4(ref object param)
IL signature .method public hidebysig instance void M4(object& param) cil managed
Method parameter description:
Is passed by reference True
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=False
IsRetVal=False


Method name M5 void M5([In] object param)
IL signature .method public hidebysig instance void M5([in] object param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=False
IsIn=True
IsLcid=False
IsOptional=False
IsOut=False
IsRetVal=False


Method name M6 void M6([Out] object param)
IL signature .method public hidebysig instance void M6([out] object param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=True
IsRetVal=False


Method name M7 void M7([Out] out object param)
IL signature .method public hidebysig instance void M7([out] object& param) cil managed
Method parameter description:
Is passed by reference True
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=True
IsRetVal=False


Method name M8 void M8([DefaultValue(null)] object param)
IL signature .method public hidebysig instance void M8(object param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=False
IsOut=False
IsRetVal=False


Method name M9 void M9([DefaultValue(-10)] int param = 10)
IL signature .method public hidebysig instance void M9([opt] int32 param) cil managed
Method parameter description:
Parameter name param
Is passed by reference False
HasDefaultValue=True
DefaultValue=10
IsIn=False
IsLcid=False
IsOptional=True
IsOut=False
IsRetVal=False


Method name M10 void M10([Optional] int param)
IL signature .method public hidebysig instance void M10([opt] int32 param) cil managed
Method parameter description:
Is passed by reference False
HasDefaultValue=False
IsIn=False
IsLcid=False
IsOptional=True
IsOut=False
IsRetVal=False

I guess In, Out and Optional attributes relate to COM as they are located in System.Runtime.InteropServices namesapce.
But again documentation is quite poor. :(

And what is RetVal and where it is used?

Était-ce utile?

La solution

This shows most of them, including the optional, out and defaultvalue flags. Note that the return value can be represented as a ParameterInfo because it shares most things in common with parameters, including attributes (via the [return: Foo] syntax). For retval and lcid, see When are ParameterInfo.IsLcid or ParameterInfo.IsRetval true?

static class Program
{
    static void Main()
    {
        var method = typeof(Program).GetMethod("Test");

        Describe(method.ReturnParameter);
        foreach (var p in method.GetParameters()) Describe(p);
    }
    static void Describe(ParameterInfo param)
    {
        Console.WriteLine("{0}, {1}, {2}",
            string.IsNullOrEmpty(param.Name) ? "(no name)" : param.Name,
            param.ParameterType, param.Position);
        if (param.IsRetval) Console.WriteLine("retval");
        if (param.IsIn) Console.WriteLine("in");
        if (param.IsOut) Console.WriteLine("out");
        if (param.ParameterType.IsByRef) Console.WriteLine("by-ref");
        if (param.IsOptional) Console.WriteLine("optional");
        if (param.HasDefaultValue)
        {
            Console.WriteLine("default value: {0}", param.DefaultValue);
        }
        Console.WriteLine();
    }

    public static int Test(int j, ref int k, out int l, string foo = "abc")
    {
        throw new NotImplementedException();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top