using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) {
var converter = new CsConverter();
converter.Run();
Console.WriteLine("ready.");
Console.Read();
}
}
public class CsConverter
{
public CSharpAstResolver resolver;
public void Run() {
CSharpProjectContent project = new CSharpProjectContent();
//cnt.AddAssemblyReferences(...)
var asmList = new List<IUnresolvedAssembly>();
var loader = new CecilLoader();
asmList.Add(loader.LoadAssemblyFile(typeof(object).Assembly.Location));
asmList.Add(loader.LoadAssemblyFile(typeof(System.Uri).Assembly.Location));
asmList.Add(loader.LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location));
project = (CSharpProjectContent)project.AddAssemblyReferences(asmList.ToArray());
CSharpParser parser = new CSharpParser();
CompilationUnit unit = parser.Parse(System.IO.File.ReadAllText("test.cs"), "test.cs");
var parsedFile = unit.ToTypeSystem();
project = (CSharpProjectContent)project.UpdateProjectContent(null, parsedFile);
SimpleCompilation compilation = (SimpleCompilation)project.CreateCompilation();
resolver = new CSharpAstResolver(compilation, unit, parsedFile);
//var visitor = new MyAstVisitor(resolver);
//unit.AcceptVisitor(visitor);
foreach (AstNode node in new List<AstNode>(unit.Descendants)) {
if (node is Expression) {
var expr = (Expression)node;
checkCast(expr);
}
}
Console.WriteLine(unit.GetText());
}
public void checkCast(Expression expr) {
var isValid = resolver.GetConversion(expr).IsValid;
if (!isValid) {
var expectedType = resolver.GetExpectedType(expr);
var rr = resolver.Resolve(expr);
if (!rr.IsError)
if (expectedType != rr.Type) {
var placeHolder = new EmptyExpression();
expr.ReplaceWith(placeHolder);
var newExp = getCast(expr, rr.Type);
placeHolder.ReplaceWith(newExp);
}
}
}
public Expression getCast(Expression expr, IType toType) {
return new InvocationExpression(new MemberReferenceExpression(expr, "ToString"));
}
}
}
//#################################################################
// test.cs
//#################################################################
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace test
{
static class Program
{
public static void Main() {
//Use of nested class
TNestedList<int> list = new TNestedList<int>();
string num = list[0]; //resolver.Resolve do not work
num = list2.Count; //resolver.Resolve do not work
//no nested class
TList<int> list2 = new TList<int>();
string num2 = list2[0];
num2 = list2.Count;
}
public class TNestedList<T>
{
public T this[int index] {
get {
return default(T);
}
}
public int Count {
get {
return 0;
}
}
}
}
public class TList<T>
{
public T this[int index] {
get {
return default(T);
}
}
public int Count {
get {
return 0;
}
}
}
}