这是我的代码:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
CommandBar SECommandBar = ((CommandBars)_applicationObject.CommandBars)["Context Menus"];
CommandBarPopup SEPopUps = (CommandBarPopup)SECommandBar.Controls["Project and Solution Context Menus"];
CommandBarPopup ooCommandBar = (CommandBarPopup)SEPopUps.Controls["Project"];
CommandBarPopup oCommandBar = (CommandBarPopup)SEPopUps.Controls["Item"];
CommandBarControl oControl = (CommandBarControl)
oCommandBar.Controls.Add(MsoControlType.msoControlButton,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value, 1, true);
// Set the caption of the menuitem
oControl.Caption = "Create Documentation";
oSubMenuItemHandler = _applicationObject.Events.get_CommandBarEvents(oControl) as CommandBarEventsClass;
oSubMenuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(oSubMenuItemHandler_Click);
}
}
public void oSubMenuItemHandler_Click(object CommandaBarControl, ref bool handled, ref bool cancelDefault)
{
SelectedItems doc = _applicationObject.SelectedItems;
// i want to get type of selected Class
}
我可以这样做吗? 任何人帮助我访问所选类的类型以制作反射和获取所有方法和属性
答案 0 :(得分:3)
CS 文件名与其中的类型无关。
这不是Java
,其中编译器假装每个文件都有一个类,而类名为文件本身(如果我没弄错的话)。
所以,不,你不能在C#
中做到这一点。
如果您需要阅读C#
编码文件,可以Roslyn为您制作该文件:
可以看一下这里Read a .cs file, add property to class, write back to .cs file的具体例子。
答案 1 :(得分:0)
为了做到这一点,你必须以编程方式编译.cs文件,这不是一项小任务。
上面有一篇不错的msdn文章:"How to programmatically compile code using C# compiler"
编译完成后,您可以使用反射从生成的程序集中获取类型:"Assembly.GetTypes Method"