1

我已经创建了 ac# 类,我试图使用 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;}         

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


@Philip 在我的脚本中,它的静态或非静态并不重要,因为它总是只运行一次,结果将用于我的程序。但奇怪的是,如果我使用静态语法(除了我根本没有得到任何结果),它似乎确实有效,如果我使用非静态语法,我会得到同样的错误。我还在互联网上找到的另一个 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

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

4

1 回答 1

1

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

$MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate

创建 的新实例ComponentSubtypeMustMatchTemplate

$MyProblemCollection.Validate()

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

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

$result = [ComponentSubtypeMustMatchTemplate]::Validate()

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

于 2010-09-22T12:01:14.117 回答