C++ cli C++/CLI-将System::Object转换为ContextMenuStrip

C++ cli C++/CLI-将System::Object转换为ContextMenuStrip,c++-cli,C++ Cli,我正在编写一段遗留代码&我正在尝试更新一些接口。我不精通C++/CLI,而且C++/CLI的文档充其量也很少。我尽最大努力将C#文档转换为C++/CLI,但并不总是有效 我想将System::Object转换为ContextMenuStrip 示例代码是: System::Void Form1::unzoomToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { System::Windows::F

我正在编写一段遗留代码&我正在尝试更新一些接口。我不精通C++/CLI,而且C++/CLI的文档充其量也很少。我尽最大努力将C#文档转换为C++/CLI,但并不总是有效

我想将System::Object转换为ContextMenuStrip

示例代码是:

System::Void Form1::unzoomToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{

    System::Windows::Forms::ContextMenuStrip ^menu = sender;
    //a value of type "System::Object ^" cannot be used to initialize and entity of type "System::Windows::Forms::ContextMenuStrip ^"

    //Other code here
}

这是如何在C++/CLI中实现的?

来自Hans Passant发布的链接:

向下转换是从基类到派生自基类的类的转换。只有在运行时寻址的对象实际上正在寻址派生类对象时,向下转换才是安全的。与静态_cast不同,安全_cast执行动态检查,并在转换失败时抛出InvalidCastException

因此,您应该使用:

Windows::Forms::ContextMenuStrip ^menu = safe_cast<Windows::Forms::ContextMenuStrip^>(sender);
Windows::Forms::ContextMenuStrip^menu=safe\u cast(发送方);

来自Hans Passant发布的链接:

向下转换是从基类到派生自基类的类的转换。只有在运行时寻址的对象实际上正在寻址派生类对象时,向下转换才是安全的。与静态_cast不同,安全_cast执行动态检查,并在转换失败时抛出InvalidCastException

因此,您应该使用:

Windows::Forms::ContextMenuStrip ^menu = safe_cast<Windows::Forms::ContextMenuStrip^>(sender);
Windows::Forms::ContextMenuStrip^menu=safe\u cast(发送方);

您可以尝试以下
自动菜单=(System::Windows::Forms::ContextMenuStrip^)发送方。您可以尝试以下
自动菜单=(系统::Windows::窗体::ContextMenuStrip^)发件人