Posted 11 years ago
by Nicolas
Version: 13.1.0583
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
I use the SyntaxEditor code fragment feature to design a "C# expression control".
My control need to resolve the type of the edited expression.
But it seems that resolution doesn't work on AST part corresponding to footer code.
Here's a simple test :
static void Main(string[] args)
{
CSharpSyntaxLanguage lang = new CSharpSyntaxLanguage();
ProjectAssembly projectAssembly = new CSharpProjectAssembly("Test");
projectAssembly.AssemblyReferences.AddMsCorLib();
lang.RegisterProjectAssembly(projectAssembly);
CodeDocument codeDoc = new CodeDocument();
codeDoc.SetHeaderAndFooterText(@"
using System;
class Test
{
Test()
{
var a = ", @";
var b = a;
}
}");
codeDoc.SetText("\"Hello world !\"");
codeDoc.ParseDataChanged += codeDoc_ParseDataChanged;
codeDoc.Language = lang;
}
static void codeDoc_ParseDataChanged(object sender, ParseDataPropertyChangedEventArgs e)
{
ILLParseData data = e.NewValue as ILLParseData;
ConstructorBody constructorBody = data.Ast.Children[1].Children[1].Children[0].Children[1] as ConstructorBody;
LocalVariableDeclaration varDecl = constructorBody.Statements.Last() as LocalVariableDeclaration;
VariableDeclarator declarator = varDecl.Declarators.Last();
Expression exp = declarator.Initializer; // expression is 'a'
TextSnapshotOffset snapshotOffset = new TextSnapshotOffset(data.Snapshot, exp.StartOffset.Value);
IDotNetContext context = new CSharpContextFactory().CreateContext(snapshotOffset, DotNetContextKind.Self);
IResolverResultSet resultSet = context.Resolve();
Debug.Assert(resultSet.Results.Count==1); // assertion fail !
Debug.Assert(resultSet.Results[0].Type.QualifiedName.ToString() == "global::System.String");
}