2012-07-25 23 views
0

我正在開發一個將自定義的C#.DLL集成到PowerShell中的項目。但是,我遇到問題 ,它將C#對象轉換爲PowerShell可以理解它無法理解的形式。我搜索了一百萬次google ,嘗試了一些不同的東西,但沒有一個成功。在C#DLL中定義的PowerShell中使用函數時發生Casting錯誤

當我打電話SNC-getVlan與對象數組發生以下錯誤:

「異常調用‘printObject’與‘1’的參數(一個或多個):」無法投型的對象' System.Management.Automation.PSObject爲鍵入」 Objects.blablazzz.DC_Object'」

我張貼我的代碼一些小的子集,希望你們能看到我在做什麼錯。

類我正在使用:

public class DC_Object 
{ 
    public string name = "undefined"; 
} 

public class Cmdb_Host : DC_Object 
{ 
    //Lots of properties 
} 

public class Cmdb_Vlan : DC_Object 
{ 
    //Lots of properties 
} 

功能,打印的對象:

public static void printObject(Object[] objects) 
{ 
    foreach (Object o in objects) 
    { 
     string name = ((DC_Object)o).name; //I'm assuimg things go wrong in here. 

的fromJSON功能是實際返回送入printObject對象的函數,不知道 如果它很重要,但我會反正張貼。

static public Object[] fromJSON(string input) 
{ 
    //Check the string to see to what object it should convert to 
    switch (input.Substring(0, 16)) 
    { 
     //Reuest for a host (Host Table) 
     case "{\"u_cmdb_ci_host": 
      if (input[18] == '[') // We have an array 
      { 
       Container_Host c_host = JsonConvert.DeserializeObject<Container_Host>(input); 
       return c_host.u_cmdb_ci_host; 
      } 
      else // We have a single object 
      { 
       Container_Host_Single c_host = JsonConvert.DeserializeObject<Container_Host_Single>(input); 
       Container_Host h = new Container_Host(); 
       h.u_cmdb_ci_host = new Cmdb_Host[1]; 
       h.u_cmdb_ci_host[0] = c_host.u_cmdb_ci_host; 
       return h.u_cmdb_ci_host; 
      } 
     //Request for a VLAN (Network Table) 
     case "{\"cmdb_ci_ip_net": 
      Container_Vlan c_vlan = JsonConvert.DeserializeObject<Container_Vlan>(input); 
      return c_vlan.cmdb_ci_ip_network; 
    } 

    return null; 
} 

PowerShell的模塊/腳本:

#Loads in the custom DLL created for this specific project. 
[Reflection.Assembly]::LoadFrom(「C:\Users\Joey\Documents\PSScripts\DataCollector\DataCollect.dll」) 

# Creates a new Client object that handles all communication between the PowerShell module and the 
# sncdb-worker at server side. 
$client = new-object blablazzz.Sender; 
[blablazzz.Config]::Configure("C:\Users\Joey\Documents\PSScripts\DataCollector\asp4all.ini") 
$client.Connect(); 

# This functions returns a Host Machine (Virtual or Physical) in object notation to for easy post-processing 
# in PowerShell. 
Function SNC-GetHost($hostz = "blabla") 
{ 
    return $client.sendMessage([blablazzz.Parser]::getHost($hostz));  
} 

# This function returns VLAN information in object notation. This is a collection most  of the time. 
function SNC-GetVLAN($vlan = "TLS-TL2-CPH-TEST") 
{ 
    return $client.sendMessage([blablazzz.Parser]::getVlan($vlan)); 
} 
Function printObject($hostz) 
{ 
    [blablazzz.Formatter]::printObject($hostz) 
} 

PowerShell命令(DLL已經裝入):

PS C:\$variable = SNC-Get-VLAN 
PS C:\printObject($variable) 

我得注意,在使用時,我printDebug功能工作正常SNC-getHost但不起作用 SNC-getVlan,SNC-Get-Vlan返回一組值,而SNC-getHost只返回一個值(它仍然是 儘管如此,但它看起來並不像PowerShell保存在一個數組中)。

+1

只是一個快速的想法......它可以幫助你找出發生了什麼錯誤,如果你修改你的printObject例程來顯示拋出拋出異常時被拋出的對象的類型。 – 2012-07-25 13:38:04

+0

@David W,我試圖將對象類型打印到控制檯,並且遇到了一些有趣的事情。 (Console.WriteLine(o.GetType());)在Host對象上使用它時,它將SMDB_Host作爲類型返回,並且在PowerShell的Vlan對象上使用它時,它返回一個PSObject而不是SMDB_VLAN對象。當我使用PrintObject($ variable [0])和索引時,它打印出一個具有正確SMDB_VLAN類型的實例。所以我假設它與我在PowerShell中處理對象有關? – 2012-07-25 13:51:58

+0

確實看起來像這樣對我... – 2012-07-25 14:02:50

回答

0

對於可能遇到同樣問題的人。解決方法是將我的函數和參數的返回值更改爲DC_Object,因爲這是頂層封裝對象。這解決了所有的鑄造問題。

相關問題