Don't like ads? PRO users don't see any ads ;-)
Guest

NRefactory 5 nested class bug

By: Arakis on Mar 6th, 2012  |  syntax: C#  |  size: 3.71 KB  |  hits: 121  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using ICSharpCode.NRefactory.CSharp;
  7. using ICSharpCode.NRefactory.CSharp.Resolver;
  8. using ICSharpCode.NRefactory.Semantics;
  9. using ICSharpCode.NRefactory.TypeSystem;
  10. using ICSharpCode.NRefactory.TypeSystem.Implementation;
  11.  
  12. namespace ConsoleApplication1
  13. {
  14.   class Program
  15.   {
  16.  
  17.     static void Main(string[] args) {
  18.       var converter = new CsConverter();
  19.       converter.Run();
  20.  
  21.       Console.WriteLine("ready.");
  22.       Console.Read();
  23.     }
  24.  
  25.   }
  26.  
  27.   public class CsConverter
  28.   {
  29.     public CSharpAstResolver resolver;
  30.  
  31.     public void Run() {
  32.       CSharpProjectContent project = new CSharpProjectContent();
  33.       //cnt.AddAssemblyReferences(...)
  34.       var asmList = new List<IUnresolvedAssembly>();
  35.       var loader = new CecilLoader();
  36.       asmList.Add(loader.LoadAssemblyFile(typeof(object).Assembly.Location));
  37.       asmList.Add(loader.LoadAssemblyFile(typeof(System.Uri).Assembly.Location));
  38.       asmList.Add(loader.LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location));
  39.       project = (CSharpProjectContent)project.AddAssemblyReferences(asmList.ToArray());
  40.  
  41.       CSharpParser parser = new CSharpParser();
  42.       CompilationUnit unit = parser.Parse(System.IO.File.ReadAllText("test.cs"), "test.cs");
  43.       var parsedFile = unit.ToTypeSystem();
  44.       project = (CSharpProjectContent)project.UpdateProjectContent(null, parsedFile);
  45.       SimpleCompilation compilation = (SimpleCompilation)project.CreateCompilation();
  46.  
  47.       resolver = new CSharpAstResolver(compilation, unit, parsedFile);
  48.       //var visitor = new MyAstVisitor(resolver);
  49.       //unit.AcceptVisitor(visitor);
  50.  
  51.       foreach (AstNode node in new List<AstNode>(unit.Descendants)) {
  52.         if (node is Expression) {
  53.           var expr = (Expression)node;
  54.           checkCast(expr);
  55.         }
  56.       }
  57.  
  58.       Console.WriteLine(unit.GetText());
  59.     }
  60.  
  61.     public void checkCast(Expression expr) {
  62.       var isValid = resolver.GetConversion(expr).IsValid;
  63.       if (!isValid) {
  64.         var expectedType = resolver.GetExpectedType(expr);
  65.         var rr = resolver.Resolve(expr);
  66.  
  67.         if (!rr.IsError)
  68.           if (expectedType != rr.Type) {
  69.             var placeHolder = new EmptyExpression();
  70.             expr.ReplaceWith(placeHolder);
  71.             var newExp = getCast(expr, rr.Type);
  72.             placeHolder.ReplaceWith(newExp);
  73.           }
  74.       }
  75.     }
  76.  
  77.     public Expression getCast(Expression expr, IType toType) {
  78.       return new InvocationExpression(new MemberReferenceExpression(expr, "ToString"));
  79.     }
  80.  
  81.   }
  82. }
  83.  
  84. //#################################################################
  85. // test.cs
  86. //#################################################################
  87.  
  88. using System;
  89. using System.Collections;
  90. using System.Collections.Generic;
  91. using System.Data;
  92.  
  93. namespace test
  94. {
  95.  
  96.   static class Program
  97.   {
  98.     public static void Main() {
  99.       //Use of nested class
  100.       TNestedList<int> list = new TNestedList<int>();
  101.       string num = list[0]; //resolver.Resolve do not work
  102.       num = list2.Count; //resolver.Resolve do not work
  103.  
  104.       //no nested class
  105.       TList<int> list2 = new TList<int>();
  106.       string num2 = list2[0];
  107.       num2 = list2.Count;
  108.     }
  109.  
  110.     public class TNestedList<T>
  111.     {
  112.       public T this[int index] {
  113.  
  114.         get {
  115.           return default(T);
  116.         }
  117.       }
  118.  
  119.       public int Count {
  120.         get {
  121.           return 0;
  122.         }
  123.       }
  124.     }
  125.  
  126.   }
  127.  
  128.   public class TList<T>
  129.   {
  130.     public T this[int index] {
  131.  
  132.       get {
  133.         return default(T);
  134.       }
  135.     }
  136.  
  137.     public int Count {
  138.       get {
  139.         return 0;
  140.       }
  141.     }
  142.   }
  143.  
  144. }