首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >贝克霍夫TcSysManager :没有为该对象定义无参数构造函数

贝克霍夫TcSysManager :没有为该对象定义无参数构造函数
EN

Stack Overflow用户
提问于 2021-12-18 19:23:59
回答 1查看 227关注 0票数 1

我正在尝试将C#集成用于贝克霍夫的自动化接口。我从贝克霍夫的网站下载了一个例子来了解更多关于它的信息。但是,当我运行它时,我会看到一个错误,它说sysManager对象的构造函数不是无参数的。这不可能是正确的,因为贝克霍夫的网站中有几个实例使用无参数构造函数实例化这个对象。有人见过这个吗?谢谢

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TCatSysManagerLib;
using System.IO;
using System.Reflection;

namespace LinkPLCProjectTC2
{
    class Program
    {
        private static string _tpyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\Templates\\Sample.tpy";
        private static TcSysManager _sysManager;

        static void Main(string[] args)
        {
            try
            {
                _sysManager = new TcSysManager();
                _sysManager.NewConfiguration();

                ITcSmTreeItem plcNode = _sysManager.LookupTreeItem("TIPC");
                ITcSmTreeItem plc = plcNode.CreateChild(_tpyPath, 0, "", null);
                ITcSmTreeItem plcProject = _sysManager.LookupTreeItem("TIPC^Sample");

                _sysManager.SaveConfiguration(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\Templates\\Sample.tsm");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

错误信息:

System.MissingMethodException:没有为该对象定义无参数构造函数。在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型下,布尔型publicOnly、布尔型noCheck、布尔型& canBeCached、RuntimeMethodHandleInternal& ctor、布尔& bNeedSecurityCheck)在System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly的System.RuntimeType.CreateInstanceSlow(布尔型publicOnly、skipCheckThis、布尔型fillCache、StackCrawlMark& stackMark)、布尔skipCheckThis、布尔fillCache、StackCrawlMark& stackMark)的System.Activator.CreateInstance(类型类型、布尔类型)的publicOnly(类型类型)的publicOnly( args)时按下任何键继续。。。

EN

回答 1

Stack Overflow用户

发布于 2022-01-09 18:34:22

原来贝克霍夫的在线文档没有更新。下面的内容可以使用,但您必须确保在运行此控制台应用程序时打开所引用的Twincat项目。

另外,请确保将助手类导入到您的C#应用程序中,以便您可以将Twincat解决方案附加到DTE。

最后,不要忘记添加对项目的引用。您需要: envdte、envdte80、envdte90、envdte90a、envdte100和TwincatSysManagerLib。

提示,如果尝试从IO树中保存组件,请记住,并非所有子组件都可以导出。例如,负载传感器(Ep3356-0022)具有导出方法,因此将使用自动化接口API中的ExportChild()方法。但是,步进驱动器不包括在内,所以您必须使用ProduceXML()代替。

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TCatSysManagerLib;
using EnvDTE;
using System.Collections;

namespace ConsoleApp3
{
class Program
{
    public static ITcSysManager11 sysMan;
    public static EnvDTE.DTE dte;
    public static EnvDTE.Project pro;
    public static EnvDTE.Solution sol;

    static void Main(string[] args)
    {
        dte = attachToExistingDte(@"C:\Users\user\Desktop\TwinCAT_CNC\CNC_MACHINE.sln", "TcXaeShell.DTE.15.0");

        /* Attach system manager to the DTE solution's project object. */
        sol = dte.Solution;
        pro = sol.Projects.Item(1);
        sysMan = (ITcSysManager11)pro.Object;

        Console.WriteLine("Is TwinCat in Online Mode? " + sysMan.IsTwinCATStarted()); 
        Console.WriteLine("What's my target NetID? " + sysMan.GetTargetNetId()); 

        ITcSmTreeItem plc = sysMan.LookupTreeItem("TIID^Device 3 (EtherCAT)^Term 1 (EK1100)^Term 2 (EL7031)");
        //plc.ExportChild("Untitled1", @"C:\Users\user\Desktop\Untitled1.tfzip");
        Console.WriteLine(plc.ProduceXml());
    }

    public static EnvDTE.DTE attachToExistingDte(string solutionPath, string progId)
    {
        EnvDTE.DTE dte = null;
        try
        {
            Hashtable dteInstances = AI_Example.Helper.GetIDEInstances(false, progId);
            IDictionaryEnumerator hashtableEnumerator = dteInstances.GetEnumerator();

            while (hashtableEnumerator.MoveNext())
            {
                EnvDTE.DTE dteTemp = (EnvDTE.DTE)hashtableEnumerator.Value;
                if (dteTemp.Solution.FullName == solutionPath)
                {
                    Console.WriteLine("Found solution in list of all open DTE objects. " + dteTemp.Name);
                    dte = dteTemp;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        return dte;
    }
}
}

Helper类:将它导入到您的项目中,注意名称空间

代码语言:javascript
运行
复制
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.IO;
 using System.Collections;
 using System.Runtime.InteropServices;

 namespace AI_Example
 {
public class Helper
{
    [DllImport("ole32.dll")]
    public static extern int GetRunningObjectTable(int reserved, out UCOMIRunningObjectTable prot);

    [DllImport("ole32.dll")]
    public static extern int CreateBindCtx(int reserved, out UCOMIBindCtx ppbc);

    public static string getFreeTcDirectory(string basePath)
    {
        int max = 1;
        List<string> directories = new List<string>(Directory.EnumerateDirectories(basePath));
        foreach (var directory in directories)
        {
            string[] dirNames = directory.Split('\\');
            if (dirNames[dirNames.Length - 1].Contains("TwinCAT Project"))
            {
                string number = dirNames[dirNames.Length - 1].Substring(15, dirNames[dirNames.Length - 1].Length - 15);
                if (Convert.ToInt32(number) > max)
                    max = Convert.ToInt32(number);
            }
        }
        max++;

        return "TwinCAT Project" + max.ToString();
    }

    /// <summary>
    /// Get a snapshot of the running object table (ROT).
    /// </summary>
    /// <returns>A hashtable mapping the name of the object
    //     in the ROT to the corresponding object</returns>
    public static Hashtable GetRunningObjectTable()
    {
        Hashtable result = new Hashtable();

        int numFetched;
        UCOMIRunningObjectTable runningObjectTable;
        UCOMIEnumMoniker monikerEnumerator;
        UCOMIMoniker[] monikers = new UCOMIMoniker[1];

        GetRunningObjectTable(0, out runningObjectTable);
        runningObjectTable.EnumRunning(out monikerEnumerator);
        monikerEnumerator.Reset();

        while (monikerEnumerator.Next(1, monikers, out numFetched) == 0)
        {
            UCOMIBindCtx ctx;
            CreateBindCtx(0, out ctx);

            string runningObjectName;
            monikers[0].GetDisplayName(ctx, null, out runningObjectName);

            object runningObjectVal;
            runningObjectTable.GetObject(monikers[0], out runningObjectVal);

            result[runningObjectName] = runningObjectVal;
        }

        return result;
    }


    /// <summary>
    /// Get a table of the currently running instances of the Visual Studio .NET IDE.
    /// </summary>
    /// <param name="openSolutionsOnly">Only return instances
    ///                   that have opened a solution</param>
    /// <returns>A hashtable mapping the name of the IDE
    ///       in the running object table to the corresponding
    ///                                  DTE object</returns>
    public static Hashtable GetIDEInstances(bool openSolutionsOnly, string progId)
    {
        Hashtable runningIDEInstances = new Hashtable();
        Hashtable runningObjects = GetRunningObjectTable();

        IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
        while (rotEnumerator.MoveNext())
        {
            string candidateName = (string)rotEnumerator.Key;
            if (!candidateName.StartsWith("!" + progId))
                continue;

            EnvDTE.DTE ide = rotEnumerator.Value as EnvDTE.DTE;
            if (ide == null)
                continue;

            if (openSolutionsOnly)
            {
                try
                {
                    string solutionFile = ide.Solution.FullName;
                    if (solutionFile != String.Empty)
                    {
                        runningIDEInstances[candidateName] = ide;
                    }
                }
                catch { }
            }
            else
            {
                runningIDEInstances[candidateName] = ide;
            }
        }
        return runningIDEInstances;
    }
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70406414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档