Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[CodeDomConvertVisitor] fixed converting fully-qualified references t…

…o static members
  • Loading branch information...
commit 76f3da7ccf51d3e009bb962a932d0b1c032a5ac5 1 parent 6efd3b9
@dgrunwald dgrunwald authored
View
10 ICSharpCode.NRefactory.CSharp/OutputVisitor/CodeDomConvertVisitor.cs
@@ -38,18 +38,19 @@ namespace ICSharpCode.NRefactory.CSharp
/// </remarks>
public class CodeDomConvertVisitor : IAstVisitor<CodeObject>
{
- //ICompilation compilation = MinimalResolveContext.Instance;
CSharpAstResolver resolver;
/// <summary>
- /// Gets/Sets whether the visitor should use fully-qualified type references.
+ /// Gets/Sets whether the visitor should convert short type names into
+ /// fully qualified type names.
+ /// The default is <c>false</c>.
/// </summary>
public bool UseFullyQualifiedTypeNames { get; set; }
/// <summary>
/// Gets whether the visitor is allowed to produce snippet nodes for
/// code that cannot be converted.
- /// The default is true. If this property is set to false,
+ /// The default is <c>true</c>. If this property is set to <c>false</c>,
/// unconvertible code will throw a NotSupportedException.
/// </summary>
public bool AllowSnippetNodes { get; set; }
@@ -464,8 +465,11 @@ CodeObject IAstVisitor<CodeObject>.VisitMemberReferenceExpression(MemberReferenc
CodeExpression target = Convert(memberReferenceExpression.Target);
ResolveResult rr = Resolve(memberReferenceExpression);
MemberResolveResult mrr = rr as MemberResolveResult;
+ TypeResolveResult trr = rr as TypeResolveResult;
if (mrr != null) {
return HandleMemberReference(target, memberReferenceExpression.MemberName, memberReferenceExpression.TypeArguments, mrr);
+ } else if (trr != null) {
+ return new CodeTypeReferenceExpression(Convert(trr.Type));
} else {
if (memberReferenceExpression.TypeArguments.Any() || rr is MethodGroupResolveResult) {
return new CodeMethodReferenceExpression(target, memberReferenceExpression.MemberName, Convert(memberReferenceExpression.TypeArguments));
View
26 ICSharpCode.NRefactory.Tests/CSharp/CodeDomConvertVisitorTests.cs
@@ -50,6 +50,12 @@ public override void SetUp()
}
#region Helper methods
+ CodeObject ConvertInContext(string program)
+ {
+ var p = PrepareResolver(program);
+ return convertVisitor.Convert(p.Item2, p.Item1);
+ }
+
string ConvertHelper(AstNode node, Action<CSharpCodeProvider, CodeObject, TextWriter, CodeGeneratorOptions> action)
{
CSharpResolver resolver = new CSharpResolver(compilation);
@@ -225,6 +231,26 @@ public void StaticProperty()
}
[Test]
+ public void FullyQualifiedEnumFieldAccess()
+ {
+ string program = "class A { object x = $System.StringComparison.Ordinal$; }";
+ var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
+ Assert.AreEqual("Ordinal", cfre.FieldName);
+ var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
+ Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
+ }
+
+ [Test]
+ public void EnumFieldAccess()
+ {
+ string program = "using System; class A { object x = $StringComparison.Ordinal$; }";
+ var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
+ Assert.AreEqual("Ordinal", cfre.FieldName);
+ var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
+ Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
+ }
+
+ [Test]
public void InstanceMethodInvocation()
{
Assert.AreEqual("this.Equals(null)", ConvertExpression("Equals(null)"));
Please sign in to comment.
Something went wrong with that request. Please try again.