ParameterInfo Method Properties, Attributes, and Parameter Parameters

Well, the properties of the ParameterInfo class are confusing me.
Unfortunately, the documentation is not very clear: the examples show how to create methods, but they don’t show how these methods look in C #.

Someone will tell Kana more about these properties:

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

And what combination leads to what method parameters.
I made a simple program that gives the following result:

Method Name M1 void M1(object param)
IL Signature: .method public hidebysig instance void M1(object param) cil managed
Parameter parameter description:
Follows the False link
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
Parameter parameter description:
Follows the False link
HasDefaultValue = True
DefaultValue = zero
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
Parameter parameter description:
Submitted by True link
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
Parameter parameter description:
Submitted by True link
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
Parameter parameter description:
Follows the False link
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
Parameter parameter description:
Follows the False link
HasDefaultValue = False
ISIN = False
IsLcid = False
IsOptional = False
IsOut = True
IsRetVal = False


Method name M7 void M7([Out] out object param)
Signature IL .method public hidebysig instance void M7([out] object& param) cil managed
Parameter parameter description:
Submitted by True link
HasDefaultValue = False
ISIN = False
IsLcid = False
IsOptional = False
IsOut = True
IsRetVal = False


Method Name M8 void M8([DefaultValue(null)] object param)
Signature IL .method public hidebysig instance void M8(object param) cil managed
Parameter parameter description:
Follows the False link
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
Parameter parameter description:
Param parameter name
Follows the False link
HasDefaultValue = True
DefaultValue = 10
ISIN = False
IsLcid = False
IsOptional = True
IsOut = False
IsRetVal = False


Method Name M10 void M10([Optional] int param)
Signature IL .method public hidebysig instance void M10([opt] int32 param) cil managed
Parameter parameter description:
Follows the False link
HasDefaultValue = False
ISIN = False
IsLcid = False
IsOptional = True
IsOut = False
IsRetVal = False

I think the In , Out and Optional attributes are for COM since they are in the System.Runtime.InteropServices namesapce.
But again, the documentation is pretty bad .:(

What is RetVal and where is it used?

+6
source share
1 answer

This shows most of them, including optional flags, out and defaultvalue. Note that the return value can be represented as ParameterInfo because it shares most of the commonalities with parameters, including attributes (via the syntax [return: Foo] ). For retval and lcid see When is 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(); } } 
+2
source

All Articles