2010-09-22 36 views
1

我做了一个c#类,我试图用add-Type在PowerShell中运行。 我有几个我自己的程序集引用,一些来自.net4。我使用我自己的PowerShell主机,因为我所引用的程序集是在.net4框架中生成的,而PowerShell控制台目前还不支持。Add-Type PowerShell 2.0找不到方法

这里是我想要运行的脚本样本:

$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]没有按“T包含一个名为方法‘验证’。“}

香港专业教育学院还试图以不同的方式在最后一位(发现如何做到这一点许多不同的例子),但他们似乎都给予同样的错误。我真的不知道如何使这个工作,我似乎无法找到任何类似的例子。

在第二个音符(因为当我得到这个固定的),我想知道是否有可能能够只加载.cs文件而不是把C#代码的脚本。会更好阅读,更易于维护。

伊夫试着看能不能看到获取会员的方法-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控制台我在interwebs发现,在那里我得到一个完全不同的错误:

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,它自发地工作!奇怪,但我很高兴! =)

回答

1

你的方法是静态的,但你正在试图通过一个实例来调用它。

$MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate 

创建新实例的ComponentSubtypeMustMatchTemplate

$MyProblemCollection.Validate() 

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

你也可以制作方法非静态(其中,如果他们要保持状态的对象,你会怎么做),或者你可以使用用于调用静态方法不同的PowerShell语法:

$result = [ComponentSubtypeMustMatchTemplate]::Validate() 

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

+0

Thx,但似乎只工作Partly =) – Ruud 2010-09-22 13:06:32

+0

嗯nvm,它自发地工作!奇怪,但我很高兴! =) – Ruud 2010-09-22 14:01:54