2012-07-25 58 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,因为这是顶层封装对象。这解决了所有的铸造问题。

相关问题