最新消息:20210917 已从crifan.com换到crifan.org

【未解决】Cannot take the address of, get the size of, or declare a pointer to a managed type xxx

C# crifan 2163浏览 0评论

【问题】

在C#中,想要把一个自定义的form变量:

downloadSongtasteMusic.frmDownloadSongtasteMusic

的指针,当做一个函数的参数,以此实现窗体指针变量的传递,结果出错:

Error    1    Cannot take the address of, get the size of, or declare a pointer to a managed type (‘downloadSongtasteMusic.frmDownloadSongtasteMusic’)    D:\tmp\tmp_dev_root\downloadSongtasteMusic\downloadSongtasteMusic\downloadSongtasteMusic\completeHint.cs    22

cannot use frmDownloadSongtasteMusic pointer

 

【解决过程】

1.参考:

Cannot take the address of, get the size of, or declare a pointer to a managed type?

试试使用IntPtr,以及后来参考:

Pointers in .net

去添加unsafe,使用如下代码:

    public partial class completeHint : Form
    {
        private string curFullFilename;
        private string curFolderPath;
        private IntPtr* curParentForm;

        public completeHint()
        {
            InitializeComponent();
        }

        public unsafe void initParameter(IntPtr* parentForm, string fullFilename, string folderPath)
        //public void initParameter(string fullFilename, string folderPath)
        {
            
            curFullFilename = fullFilename;
            curFolderPath = folderPath;
            curParentForm = (frmDownloadSongtasteMusic *)parentForm;

            //Control curParent = this.Parent;
            //Form foundParentForm = curParent.FindForm();
            //curParentForm = (frmDownloadSongtasteMusic)foundParentForm;
            //curParentForm.resetHintWindow();
        }

但是结果还是出错,还是无法强制转换为所需要的frmDownloadSongtasteMusic变量。

 

【总结】

暂时放弃此条路。

换用其他方式,实现:

C#中,如何设置另外一个窗口,或者父窗口,中的值

转载请注明:在路上 » 【未解决】Cannot take the address of, get the size of, or declare a pointer to a managed type xxx

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.209 seconds, using 20.12MB memory