views:

61

answers:

3

I'm trying to evaluate C# code as it gets typed, think of it as if I'm trying to write an IDE.

So a person types code, I want to find out what code did he just write:

String x = "";

I want to now register that x is a type of String. And now everytime the user types x again, and I want to show him all the things he can do with x, basically like Visual Studio Intellisense.

Will I need some lexers or parsers for this? Will that make it easier? I've heard VS 2010 has some features around this that Microsoft has released. Any ideas?

A: 

You can use the DLR to help with this. See DLRPad on codeplex for an example

If you use the DLR interrogate types in the CLR more easy than using reflection in C#. last time I looked (about 9 months ago) I thought the intellisense had been implemented using this technique as we were going to do it the same way.

Preet Sangha
Can you explain what exactly in DLRPad, I read a bit and it seems like it's mostly got to the with IRonPython, etc. how does it apply here?
halivingston
A: 

I would avoid trying to write this yourself if you can. SharpDevelop is an open source .NET IDE which has Intellisense-like functionality. I believe you can embed their editor in your own project. According to Wikipedia, the license is LGPL so you could probably use it even in closed source projects. Of course, you should verify this for yourself before doing so.

Actipro SyntaxEditor is a third party control you can buy which does this. There are WinForms and WPF versions of the control. It has worked well for us. It doesn't currently support Intellisense with LINQ and lambda expressions. I don't know if SharpDevelop supports this or not.

Daniel Plaisted
Ok, well the idea really isn't IntelliSense, but something else. I used the word to give the point across. I really need to be able to take a line of code and figure out details like type returned, basically evaluate an expression.Now that expression can depend on variables declared earlier, but that's it, it's as complex as building a symbol table, and then some more stuff. I honestly though .NET 4.0 had some infrastructre that let me do this.
halivingston
A: 

I don't think .NET 4 has anything to help with this. You might look at Mono REPL. It also might help if you give more details about what you actually want to do.

Daniel Plaisted
The intent is to evaluate an expression for type, and other details like instance or static-ness.For example,String x = String.Empty;Now x is a type of String, and is an instance variable, so I'd like to show instance-related actions. Now it seems like I'm trynig to implement IntelliSense, and mostly yes, but not in the same UI, different way, to help code searching.I've got most of the trivial stuff working, the problem comes for things like:x.ToString().ToUpper() // starts getting complex.So a lexer/parser might help here.
halivingston
@halivingston I still don't really understand what you're trying to do. What is the user of the application going to be doing with the code? Are they going to be editing and running it? Or is this a way to explore existing code? Does it have something to do with searching a codebase? Is this a tool to refactor code?
Daniel Plaisted