I wrote few tests to see the what happens behind the scene.
Tests.
1. Reflecting Optional with default values
public void MethodWithDefaultValue(string name ="N/A", int cound =50) { }
public void MethodWithDeffaults([Optional, DefaultParameterValue("N/A")] string name, [Optional, DefaultParameterValue(50)] int count) { }
2. Reflecting Optional parametes when ommitted : When above method is called
public void ReflectMethodSignatureForOptionalWithDefaultValue() { ReflectClass optionalWithDefaultValue = new ReflectClass(); optionalWithDefaultValue.MethodWithDefaults(); }
public void ReflectMethodSignatureForOptionalWithDefaultValue() { new ReflectClass().MethodWithDefaults("N/A", 50); }
3. Reflecting Named Parameters: if method in Test 1 called with named parameters,
public void ReflectNamedParametersCallerCode() { ReflectClass optionalWithDefaultValue = new ReflectClass(); optionalWithDefaultValue.MethodWithDefaults(count: 20, name: "Hello"); }
public void ReflectNamedParametersCallerCode() { ReflectClass optionalWithDefaultValue = new ReflectClass(); int CS$0$0000 = 20; string CS$0$0001 = "Hello"; optionalWithDefaultValue.MethodWithDefaults(CS$0$0001, CS$0$0000); }
Complete Listing: In case you want to see yourself in reflector. Mouse hover on the listing to see clipboard/view source options.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace episodes { public class ReflectingNamedOptionalParams { public void ReflectMethodSignatureForOptionalWithDefaultValue() { ReflectClass optionalWithDefaultValue = new ReflectClass(); optionalWithDefaultValue.MethodWithDefaults(); } public void ReflectNamedParametersCallerCode() { ReflectClass optionalWithDefaultValue = new ReflectClass(); optionalWithDefaultValue.MethodWithDefaults(count: 20, name: "Hello"); } } public class ReflectClass { public void MethodWithDefaults(string name="N/A",int count=50) { } } }