【问题标题】:Method not found: 'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)'找不到方法:'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)'
【发布时间】:2021-02-11 21:28:43
【问题描述】:

我花了很多时间在我的开发箱上编写这个程序,在将它移到我们的生产箱后,我收到以下错误。仅供参考,我无法控制安装的内容和可以安装的内容,我该如何进行这项工作?在两台计算机上的两个框架下,我们都有 v1.0.3705、v1.1.4322、v2.0.50727、v3.0、v3.5、4.0.30319。另外,我用来创建应用程序的程序是 Visual Studio 2013 Pro。

谢谢

有关调用即时的详细信息,请参阅此消息的末尾 (JIT) 调试而不是此对话框。

*****异常文本** ***** System.MissingMethodException:找不到方法:'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)'。 在 C_Sharp_version.Form1.button4_Click(对象发送者,EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

***** 加载的程序集** ***** mscorlib 程序集版本:4.0.0.0 Win32 版本:4.0.30319.239 (RTMGDR.030319-2300) 代码库:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll ---------------------------------------------------- C Sharp 版本 程序集版本:1.0.0.0 Win32版本:1.0.0.0 代码库:file:///gordonc$/Desktop/C%20Sharp%20version%20(2).exe ---------------------------------------------------- System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.0.30319.235 构建者:RTMGDR 代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------------------- System.Drawing 程序集版本:4.0.0.0 Win32 版本:4.0.30319.1 构建者:RTMRel 代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll - - - - - - - - - - - - - - - - - - - - 系统 程序集版本:4.0.0.0 Win32 版本:4.0.30319.236 构建者:RTMGDR 代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;


namespace C_Sharp_version
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
        {
            //options
            object matchCase = false;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;
            //execute find and replace
            doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
                ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openInfDoc = new OpenFileDialog();

            if (openInfDoc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strInfDocLocation = openInfDoc.FileName;

                MessageBox.Show(strInfDocLocation);
                InfDocLocationtxt.Text = strInfDocLocation; 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openNetdoc = new OpenFileDialog();
            if (openNetdoc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strNetDocLocation = openNetdoc.FileName;

                MessageBox.Show(strNetDocLocation);
                NetDocLocationtxt.Text = strNetDocLocation;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openChkdoc = new OpenFileDialog();
            if (openChkdoc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strChkDocLocation = openChkdoc.FileName;

                MessageBox.Show(strChkDocLocation);
                ChkDocLocationtxt.Text = strChkDocLocation;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, InfDocLocationtxt.Text);
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);
            aDoc.Activate();
            FindAndReplace(wordApp, "<ServerName>", textBox1.Text);
            FindAndReplace(wordApp, "<Domain>", textBox2.Text);
            FindAndReplace(wordApp, "<Ip>", textBox3.Text);
            FindAndReplace(wordApp, "<Engineer>", textBox4.Text);
            FindAndReplace(wordApp, "<Date>", textBox5.Text);
            FindAndReplace(wordApp, "<Project>", textBox6.Text);
            FindAndReplace(wordApp, "<Cluster>", textBox7.Text);
            FindAndReplace(wordApp, "<DocNumber>", textBox8.Text);
            FindAndReplace(wordApp, "<Author>", textBox9.Text);


        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, NetDocLocationtxt.Text);
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);
            aDoc.Activate();
            FindAndReplace(wordApp, "<ServerName>", textBox1.Text);
            FindAndReplace(wordApp, "<Domain>", textBox2.Text);
            FindAndReplace(wordApp, "<Ip>", textBox3.Text);
            FindAndReplace(wordApp, "<Engineer>", textBox4.Text);
            FindAndReplace(wordApp, "<Date>", textBox5.Text);
            FindAndReplace(wordApp, "<Project>", textBox6.Text);
            FindAndReplace(wordApp, "<Cluster>", textBox7.Text);
            FindAndReplace(wordApp, "<DocNumber>", textBox8.Text);
            FindAndReplace(wordApp, "<Author>", textBox9.Text);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, ChkDocLocationtxt.Text);
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);
            aDoc.Activate();
            FindAndReplace(wordApp, "<ServerName>", textBox1.Text);
            FindAndReplace(wordApp, "<Domain>", textBox2.Text);
            FindAndReplace(wordApp, "<Ip>", textBox3.Text);
            FindAndReplace(wordApp, "<Engineer>", textBox4.Text);
            FindAndReplace(wordApp, "<Date>", textBox5.Text);
            FindAndReplace(wordApp, "<Project>", textBox6.Text);
            FindAndReplace(wordApp, "<Cluster>", textBox7.Text);
            FindAndReplace(wordApp, "<DocNumber>", textBox8.Text);
            FindAndReplace(wordApp, "<Author>", textBox9.Text);
        }


    }
}

【问题讨论】:

    标签: c# interop visual-studio-2013


    【解决方案1】:

    在我看来这是一个 .NET Framework compatibility 问题。

    问题:您已经在本地计算机上使用更高版本的.NET Framework 开发了您的应用程序,并在具有更低版本.NET Framework 的远程电脑上运行相同的应用程序。

    注意:如果您的应用程序目标是在 Heigher 版本的 .NET Framework 上运行,则它不会在较低版本上运行。

    解决方案:您需要将其定位到远程电脑上可用的.NET Framework 较低版本才能在远程电脑上运行。

    第一步:右击project - 选择properties

    第 2 步:Target Framework.NET Framework x.x 更改为 .NET Framework x.y

    注意:x.x 更高,x.y 是远程电脑上可用的较低版本或任何较低版本。

    【讨论】:

    • 嗯...好的,但是如何防止这种异常发生,因为据我所知,它不能被 try/catch 语句捕获。用户不必是最新的,也不必看到应用程序因此而崩溃......
    • Oliver - 如果您在执行任何其他代码之前检查已安装的框架版本,您可以在异常发生之前“捕获”/防止异常。 Microsoft 的网站上有关于如何通过查看注册表来确定已安装版本的示例代码。 msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
    • 请注意,GetVersion 代码必须与您的其他代码分开。如果您将 GetVersion 方法插入主程序并且导致崩溃的代码在那里,您的程序仍然会崩溃。创可贴修复是创建 Main 和 RealMain。在 Main 中粘贴 GetVersion。如果版本兼容,请调用 RealMain 否则显示有用的升级框架消息...
    • 很晚了,但是...只是为了确保我理解 - 如果您将应用程序定位到较低的 .NET Framework,那么将使用 Type.GetTypeFromCLSID() 而不是 Marshal.GetTypeFromCLSID()?
    • 我在远程计算机上安装了最新版本的 .NET 框架,这个错误消失了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多