Reputation: 11489
I'm trying to add Intellisense to C# code editor based on the richtextbox control. So far, I've got it parsing the entered text to find all variables and their types (works well). The drop down box works well. What I can't get is a proper list of options for the drop-down list box.
How can I get the following list, programmatically:
I have already compiled a list of variables and their types, so when the user presses .
I know that I have a variable c
of type Color
. I just need to know what function to call to get the list I need for the drop-down box.
I've tried this code: http://www.codeproject.com/KB/cs/diy-intellisense.aspx but couldn't get it to work properly. I've also read a ton of other threads on StackOverflow to no avail. I'd really like to finish this instead of using someone elses drop-in editor component.
Any hints would be appreciated. Thanks.
Upvotes: 13
Views: 1574
Reputation: 245389
If you know the type, you should be able to Reflect on the type and get all the information you need.
Type.GetMembers would probably be your best bet. You may need a second call to get any static methods as well:
var instanceMembers = typeof(Color)
.GetMembers(BindingFlags.Instance | BindingFlags.Public);
var staticMembers = typeof(Color)
.GetMembers(BindingFlags.Static | BindingFlags.Public);
Each MemberInfo object will be able to tell you the MemberType (Property, Field, Method, Event, etc.)
Just use the instanceMembers
when the user types a variable (like c
in your example) followed by .
and use the staticMembers
when the user types a type name (like Color
in your example) followed by .
.
Upvotes: 4
Reputation: 41983
You'd want to use reflection to some degree. If you have the type, or the name of the type, you can get a Type
instance.
E.g. Type.GetType("System.Int32")
Then you can call Type.GetMembers()
on that Type
object, see here:
...and you'll have an array of MemberInfo
objects which have the name (.Name
), type of the member (.MemberType
), and from that other information, like parameter lists.
Hope that helps.
Upvotes: 1
Reputation: 78242
Assuming you have a name table with types this should give you a decent start:
var type = _names[name].Type;
var members = type.GetMembers(); // Check context to grab private methods?
So maybe you can extend your name table to include:
Type
Context
Members
Upvotes: 1