【发布时间】:2016-03-07 11:30:48
【问题描述】:
我正在尝试将以下 C# 代码转换为 PowerShell(我忽略了我认为不相关的代码部分):
C#
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0", true);
object obj = System.Activator.CreateInstance(t, true);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;
dte.MainWindow.Activate();
dynamic solution = dte.Solution;
solution.Create(solution_folder_path, solution_name);
solution.SaveAs(solution_file_path);
dynamic project = solution.AddFromTemplate(template_path, project_folder_path, project_name);
TCatSysManagerLib.ITcSysManager system_manager = project.Object;
PowerShell
[System.Reflection.Assembly]::LoadFrom("C:\Windows\assembly\GAC_MSIL\TCatSysManagerLib\2.1.0.0__180016cd49e5e8c3\TCatSysManagerLib.dll");
$dte = New-Object -ComObject VisualStudio.DTE.10.0
$dte.SuppressUI = $false
$dte.MainWindow | %{$_.GetType().InvokeMember("Visible", "SetProperty", $null, $_, $true)}
$solution = $dte.Solution
$project = $solution.AddFromTemplate($template_path, $project_folder_path, $project_name)
[TCatSysManagerLib.ITcSysManager] $system_manager = [TCatSysManagerLib.ITcSysManager] $project.Object
我遇到的问题是 PowerShell 中的最后一行返回了以下形式的错误:
无法转换类型的“System.__ComObject”值 “System.__ComObject#{3b56a5ce-0c02-440b-8ced-f1f3e83f66ed}”键入 "TCatSysManagerLib.ITcSysManager"。
添加细节:
我可以在 PowerShell 代码中省略 [TCatSysManagerLib.ITcSysManager] 类型。我在稍后转换此 C# 代码时遇到了同样的问题:
TCatSysManagerLib.ITcSmTreeItem gvl = system_manager.LookupTreeItem("TIPC^PLC1^PLC1 Project^GVLs^GVL");
TCatSysManagerLib.ITcPlcDeclaration gvl_declaration = (TCatSysManagerLib.ITcPlcDeclaration)gvl;
string gvl_declaration_text = System.IO.File.ReadAllText(gvl_declaration_text_file_path);
gvl_declaration.DeclarationText = gvl_declaration_text;
在这种情况下,我必须强制转换为TCatSysManagerLib.ITcPlcDeclaration 类型才能访问gvl_declaration 的DeclarationText 属性。
我是 C# 和 PowerShell 的新手,并且已经从示例中修改了这些内容。任何建议将不胜感激。
【问题讨论】:
标签: c# powershell com