Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#DLL中定义的PowerShell中使用函数时发生强制转换错误_C#_Dll_Powershell_Casting - Fatal编程技术网

在C#DLL中定义的PowerShell中使用函数时发生强制转换错误

在C#DLL中定义的PowerShell中使用函数时发生强制转换错误,c#,dll,powershell,casting,C#,Dll,Powershell,Casting,我正在做一个项目,将定制的c#DLL集成到powershell中。然而,我有问题 通过将C#对象转换为PowerShell可以理解但无法理解的形式。我搜索过谷歌 我试了无数次,尝试了几件不同的事情,但都没有成功 使用对象数组调用SNC getVlan时发生以下错误: 使用“1”参数调用“printObject”时出现异常:“无法将类型为“System.Management.Automation.PSObject”的对象强制转换为类型为“Objects.Blazzz.DC_object”” 我正在

我正在做一个项目,将定制的c#DLL集成到powershell中。然而,我有问题 通过将C#对象转换为PowerShell可以理解但无法理解的形式。我搜索过谷歌 我试了无数次,尝试了几件不同的事情,但都没有成功

使用对象数组调用SNC getVlan时发生以下错误:

使用“1”参数调用“printObject”时出现异常:“无法将类型为“System.Management.Automation.PSObject”的对象强制转换为类型为“Objects.Blazzz.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命令(已加载dll):

我必须注意,当在SNC getHost上使用printDebug函数时,它可以正常工作,但在SNC getHost上不工作 SNC getVlan,SNC Get Vlan返回一个值数组,而SNC getHost只返回一个值(仍然是
虽然在阵列中,但看起来不像PowerShell将其保存在阵列中).

适用于可能遇到相同问题的人。解决方法是将我的函数的返回值和参数更改为DC_对象,因为这是顶级封装对象。这解决了所有强制转换问题。

请快速思考……如果将printObject例程修改为显示抛出强制转换异常时被强制转换的对象的类型。@David W,我试图将对象类型打印到控制台,但遇到了一些有趣的事情。(console.WriteLine(o.GetType());)在主机对象上使用此选项时,它将SMDB_主机作为类型返回;在powershell中的Vlan对象上使用此选项时,它将返回一个PSObject而不是SMDB_Vlan对象。当我使用PrintObject($variable[0])时使用索引,它会打印出一个具有正确SMDB_VLAN类型的实例。因此,我假设这与我在PowerShell中处理对象有关?对我来说确实如此…尝试将参数的声明从“Object[]”更改为“DC_Object[]“@OldFrat我将declaration改为DC_Object,而不是Object,这无论如何都是正确的。在我更改了DC_对象的所有返回路径之后,一切都正常工作。非常感谢。
#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)
}
PS C:\$variable = SNC-Get-VLAN
PS C:\printObject($variable)