Skip to content
This repository
Browse code

[CodeDomConvertVisitor] fixed converting fully-qualified references t…

…o static members
  • Loading branch information...
commit 76f3da7ccf51d3e009bb962a932d0b1c032a5ac5 1 parent 6efd3b9
Daniel Grunwald authored September 08, 2012
10  ICSharpCode.NRefactory.CSharp/OutputVisitor/CodeDomConvertVisitor.cs
@@ -38,18 +38,19 @@ namespace ICSharpCode.NRefactory.CSharp
38 38
 	/// </remarks>
39 39
 	public class CodeDomConvertVisitor : IAstVisitor<CodeObject>
40 40
 	{
41  
-		//ICompilation compilation = MinimalResolveContext.Instance;
42 41
 		CSharpAstResolver resolver;
43 42
 		
44 43
 		/// <summary>
45  
-		/// Gets/Sets whether the visitor should use fully-qualified type references.
  44
+		/// Gets/Sets whether the visitor should convert short type names into
  45
+		/// fully qualified type names.
  46
+		/// The default is <c>false</c>.
46 47
 		/// </summary>
47 48
 		public bool UseFullyQualifiedTypeNames { get; set; }
48 49
 		
49 50
 		/// <summary>
50 51
 		/// Gets whether the visitor is allowed to produce snippet nodes for
51 52
 		/// code that cannot be converted.
52  
-		/// The default is true. If this property is set to false,
  53
+		/// The default is <c>true</c>. If this property is set to <c>false</c>,
53 54
 		/// unconvertible code will throw a NotSupportedException.
54 55
 		/// </summary>
55 56
 		public bool AllowSnippetNodes { get; set; }
@@ -464,8 +465,11 @@ CodeObject IAstVisitor<CodeObject>.VisitMemberReferenceExpression(MemberReferenc
464 465
 			CodeExpression target = Convert(memberReferenceExpression.Target);
465 466
 			ResolveResult rr = Resolve(memberReferenceExpression);
466 467
 			MemberResolveResult mrr = rr as MemberResolveResult;
  468
+			TypeResolveResult trr = rr as TypeResolveResult;
467 469
 			if (mrr != null) {
468 470
 				return HandleMemberReference(target, memberReferenceExpression.MemberName, memberReferenceExpression.TypeArguments, mrr);
  471
+			} else if (trr != null) {
  472
+				return new CodeTypeReferenceExpression(Convert(trr.Type));
469 473
 			} else {
470 474
 				if (memberReferenceExpression.TypeArguments.Any() || rr is MethodGroupResolveResult) {
471 475
 					return new CodeMethodReferenceExpression(target, memberReferenceExpression.MemberName, Convert(memberReferenceExpression.TypeArguments));
26  ICSharpCode.NRefactory.Tests/CSharp/CodeDomConvertVisitorTests.cs
@@ -50,6 +50,12 @@ public override void SetUp()
50 50
 		}
51 51
 		
52 52
 		#region Helper methods
  53
+		CodeObject ConvertInContext(string program)
  54
+		{
  55
+			var p = PrepareResolver(program);
  56
+			return convertVisitor.Convert(p.Item2, p.Item1);
  57
+		}
  58
+		
53 59
 		string ConvertHelper(AstNode node, Action<CSharpCodeProvider, CodeObject, TextWriter, CodeGeneratorOptions> action)
54 60
 		{
55 61
 			CSharpResolver resolver = new CSharpResolver(compilation);
@@ -225,6 +231,26 @@ public void StaticProperty()
225 231
 		}
226 232
 		
227 233
 		[Test]
  234
+		public void FullyQualifiedEnumFieldAccess()
  235
+		{
  236
+			string program = "class A { object x = $System.StringComparison.Ordinal$; }";
  237
+			var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
  238
+			Assert.AreEqual("Ordinal", cfre.FieldName);
  239
+			var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
  240
+			Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
  241
+		}
  242
+		
  243
+		[Test]
  244
+		public void EnumFieldAccess()
  245
+		{
  246
+			string program = "using System; class A { object x = $StringComparison.Ordinal$; }";
  247
+			var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
  248
+			Assert.AreEqual("Ordinal", cfre.FieldName);
  249
+			var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
  250
+			Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
  251
+		}
  252
+		
  253
+		[Test]
228 254
 		public void InstanceMethodInvocation()
229 255
 		{
230 256
 			Assert.AreEqual("this.Equals(null)", ConvertExpression("Equals(null)"));

0 notes on commit 76f3da7

Please sign in to comment.
Something went wrong with that request. Please try again.