Browse Source

Added support for explicit optional parameters.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
1d32c620f3
  1. 1
      ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs
  2. 11
      ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs
  3. 11
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs

1
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 MethodWithOutParameter(out int x) { x = 0; }
public void MethodWithParamsArray(params object[] x) {} public void MethodWithParamsArray(params object[] x) {}
public void MethodWithOptionalParameter(int x = 4) {} public void MethodWithOptionalParameter(int x = 4) {}
public void MethodWithExplicitOptionalParameter([Optional] int x) {}
public void MethodWithEnumOptionalParameter(StringComparison x = StringComparison.OrdinalIgnoreCase) {} public void MethodWithEnumOptionalParameter(StringComparison x = StringComparison.OrdinalIgnoreCase) {}
} }

11
ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs

@ -605,6 +605,17 @@ namespace ICSharpCode.NRefactory.TypeSystem
Assert.AreEqual(0, p.Attributes.Count); Assert.AreEqual(0, p.Attributes.Count);
Assert.AreEqual(4, p.ConstantValue); 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] [Test]
public void MethodWithEnumOptionalParameter() public void MethodWithEnumOptionalParameter()

11
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs

@ -190,6 +190,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
} }
return b.ToString(); return b.ToString();
} }
static bool IsOptionalAttribute (IType attributeType)
{
return attributeType.Name == "OptionalAttribute" && attributeType.Namespace == "System.Runtime.InteropServices";
}
public IParameter CreateResolvedParameter(ITypeResolveContext context) public IParameter CreateResolvedParameter(ITypeResolveContext context)
{ {
@ -205,8 +210,10 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
IsParams = this.IsParams IsParams = this.IsParams
}; };
} else { } else {
return new DefaultParameter(type.Resolve(context), name, region, var resolvedAttributes = attributes.CreateResolvedAttributes (context);
attributes.CreateResolvedAttributes(context), IsRef, IsOut, IsParams); bool isOptional = resolvedAttributes != null && resolvedAttributes.Any (a => IsOptionalAttribute (a.AttributeType));
return new DefaultParameter (type.Resolve (context), name, region,
resolvedAttributes, IsRef, IsOut, IsParams, isOptional);
} }
} }

Loading…
Cancel
Save