【问题标题】:Add-Type PowerShell 2.0 can't find methodAdd-Type PowerShell 2.0 找不到方法
【发布时间】:2011-04-15 16:24:10
【问题描述】:

我已经创建了一个 c# 类,我尝试使用 add-Type 在 PowerShell 中运行它。 我引用了一些我自己的程序集和一些来自 .net4 的程序集。我使用自己的 PowerShell 主机,因为我引用的程序集是在 .net4 框架上制作的,而 PowerShell 控制台还不支持。

这是我尝试运行的脚本示例:

$Source = @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using MyOwn.Components.Utilities;
using MyOwn.Components;
using MyOwn.Entities;

public class ComponentSubtypeMustMatchTemplate
{

    public static Guid ProblemId
    {
        get { return new Guid("{527DF518-6E91-44e1-B1E4-98E0599CB6B2}"); }
    }

    public static ProblemCollection Validate()
    {

 SoftwareStructureEntityContainer softwareEntityStructure = new SoftwareStructureEntityContainer();

        if (softwareEntityStructure == null)
        {
            throw new ArgumentNullException("softwareStructure");
        }

        ProblemCollection problems = new ProblemCollection();

        foreach (var productDependency in softwareEntityStructure.ProductDependencies)
        {......

        return problems;
    }
}
"@

$refs = @("d:\Projects\MyOwn\bin\MyOwn.Components.dll",
"d:\Projects\MyOwn\bin\MyOwn.Entities.dll",
"d:\Projects\MyOwn\bin\MyOwn.OperationalManagement.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Entity.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll")

Add-Type -ReferencedAssemblies $refs -TypeDefinition $Source


$MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate
$MyProblemCollection.Validate()

现在当我运行这个我得到这个错误:

{"方法调用失败,因为 [ComponentSubtypeMustMatchTemplate] 不包含名为 'Validate' 的方法。"}

我还以不同的方式尝试了最后一点(找到了许多不同的示例来说明如何执行此操作),但它们似乎都给出了相同的错误。我真的不知道如何让这个工作,我似乎找不到任何类似的例子。

第二个注意事项(当我修复此问题时)我想知道是否可以只加载 .cs 文件而不是将 c# 代码放入脚本中。将是更好的阅读和更可维护的。

我试着看看我是否可以看到像这样的 Get-Member -static 方法:

$MyProblemCollection = New-Object ComponentSubtypeMustMatchTemplate
$MyProblemCollection | Get-Member -Static

它确实看到了那里的方法:

   TypeName: ComponentSubtypeMustMatchTemplate

Name            MemberType Definition                                           
----            ---------- ----------                                           
Equals          Method     static bool Equals(System.Object objA, System.Obje...
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, Sy...
Validate        Method     static MyOwn.ComponentSubtypeMustMatchTemplate...  <--
ProblemId       Property   static System.Guid ProblemId {get;}         

那么为什么它不起作用!?


@菲利普 在我的脚本中,它的静态或非静态原因并不重要,它总是只运行一次,结果将用于我的程序。但奇怪的是,如果我使用静态语法,它似乎确实有效(除了我根本没有得到任何结果),如果我使用非静态语法,我会得到同样的错误。我还在互联网上找到的另一个 PowerShell 控制台中尝试了该脚本,但我得到了一个完全不同的错误:

 PS C:\temp.
ps1
The following exception occurred while retrieving member "Validate": "Could not
 load file or assembly 'MyOwn.Entit
ies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fc80acb30cba5fab' or one
of its dependencies. The system cannot find the file specified."
At D:\Projects\temp.ps1:145 char:30
+ $MyProblemCollection.Validate <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemExceptio
   n
    + FullyQualifiedErrorId : CatchFromBaseGetMember

hmm nvm,它自发地工作了!奇怪但我很高兴! =)

【问题讨论】:

    标签: c# powershell-2.0 add-type


    【解决方案1】:

    您的方法是静态的,但您正试图通过实例调用它。

    $MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate
    

    创建ComponentSubtypeMustMatchTemplate新实例

    $MyProblemCollection.Validate()
    

    $MyProblemCollection 引用的对象上调用名为Validateinstance 方法。但是,由于没有名为 Validate 的实例方法,因此调用失败。

    您可以使方法成为非静态方法(如果它们要在对象中保持状态,您会这样做),或者您可以使用不同的 powershell 语法来调用静态方法:

    $result = [ComponentSubtypeMustMatchTemplate]::Validate()
    

    请记住(如果您保留这些静态属性),设置静态属性意味着在同一进程中查询该属性的任何内容都将获得该值。如果您持有状态,那么我建议您从每个声明中删除 static

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2023-03-27
      相关资源
      最近更新 更多