简体   繁体   English

实现Visual Studio Intellisense

[英]Implementing Visual Studio Intellisense

I'm trying to add Intellisense to C# code editor based on the richtextbox control. 我正在尝试将基于richtextbox控件的Intellisense添加到C#代码编辑器中。 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 . 我知道我有一个Color类型的变量c 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. 我尝试过这段代码: http//www.codeproject.com/KB/cs/diy-intellisense.aspx但无法使其正常工作。 I've also read a ton of other threads on StackOverflow to no avail. 我还在StackOverflow上阅读了大量其他线程但无济于事。 I'd really like to finish this instead of using someone elses drop-in editor component. 我真的想完成这个而不是使用别人的插件编辑器组件。

Any hints would be appreciated. 任何提示将不胜感激。 Thanks. 谢谢。

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. Type.GetMembers可能是你最好的选择。 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.) 每个MemberInfo对象都能告诉你MemberType(属性,字段,方法,事件等)

Just use the instanceMembers when the user types a variable (like c in your example) followed by . 只需在用户键入变量时使用instanceMembers (如示例中的c ),然后使用. and use the staticMembers when the user types a type name (like Color in your example) followed by . 并在用户键入类型名称(如示例中的Color )后跟staticMembers使用staticMembers . .

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

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. 如果您具有类型或类型的名称,则可以获取Type实例。

Eg Type.GetType("System.Int32") 例如Type.GetType("System.Int32")

Then you can call Type.GetMembers() on that Type object, see here: 然后你可以在那个Type对象上调用Type.GetMembers() ,见这里:

http://msdn.microsoft.com/en-us/library/424c79hc.aspx http://msdn.microsoft.com/en-us/library/424c79hc.aspx

...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. ...并且您将拥有一个MemberInfo对象数组,这些对象具有名称( .Name ),成员类型( .MemberType )以及其他信息,例如参数列表。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM