diff --git a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs index 7d18043c5a..d233d0dd48 100644 --- a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs +++ b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs @@ -149,6 +149,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.TestCase public void MethodWithOutParameter(out int x) { x = 0; } public void MethodWithParamsArray(params object[] x) {} public void MethodWithOptionalParameter(int x = 4) {} + public void MethodWithExplicitOptionalParameter([Optional] int x) {} public void MethodWithEnumOptionalParameter(StringComparison x = StringComparison.OrdinalIgnoreCase) {} } diff --git a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs index c056c25671..b333887d13 100644 --- a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs +++ b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs @@ -605,6 +605,17 @@ namespace ICSharpCode.NRefactory.TypeSystem Assert.AreEqual(0, p.Attributes.Count); Assert.AreEqual(4, p.ConstantValue); } + + [Test] + public void MethodWithExplicitOptionalParameter() + { + IParameter p = GetTypeDefinition(typeof(ParameterTests)).Methods.Single(m => m.Name == "MethodWithExplicitOptionalParameter").Parameters.Single(); + Assert.IsTrue(p.IsOptional); + Assert.IsFalse(p.IsRef); + Assert.IsFalse(p.IsOut); + Assert.IsFalse(p.IsParams); + Assert.AreEqual(1, p.Attributes.Count); + } [Test] public void MethodWithEnumOptionalParameter() diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs index 1ca05856ac..28d8feb4ce 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs @@ -190,6 +190,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation } return b.ToString(); } + + static bool IsOptionalAttribute (IType attributeType) + { + return attributeType.Name == "OptionalAttribute" && attributeType.Namespace == "System.Runtime.InteropServices"; + } public IParameter CreateResolvedParameter(ITypeResolveContext context) { @@ -205,8 +210,10 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation IsParams = this.IsParams }; } else { - return new DefaultParameter(type.Resolve(context), name, region, - attributes.CreateResolvedAttributes(context), IsRef, IsOut, IsParams); + var resolvedAttributes = attributes.CreateResolvedAttributes (context); + bool isOptional = resolvedAttributes != null && resolvedAttributes.Any (a => IsOptionalAttribute (a.AttributeType)); + return new DefaultParameter (type.Resolve (context), name, region, + resolvedAttributes, IsRef, IsOut, IsParams, isOptional); } }