我有代码可以获取虚拟机网络连接并检索连接的交换机信息。
我的代码似乎适用于“VirtualEthernetCardNetworkBackingInfo”类型接口,但是当服务器连接到VD Switch时失败,因为需要在该场景中使用“VirtualEthernetCardDistributedVirtualPortBackingInfo”类型。
问题是,在尝试转换对象之前,如何检测接口类型?或者应该只是试图投射并看看哪个有效?
这是我的代码,但我正在寻找更清洁的东西......
bool vswitch = false, vds = false;
VirtualEthernetCard _workloadnic = (VirtualEthernetCard)_virtualdevice;
//first check if the VM is connected to a vswitch
try
{
VirtualEthernetCardNetworkBackingInfo _nic_backing = (VirtualEthernetCardNetworkBackingInfo)_workloadnic.Backing;
network_moid = _nic_backing.Network.Value;
vswitch = true;
}
catch (Exception ex) { }
//if it was not a vswitch it should be a vds
if (!vswitch)
{
try
{
VirtualEthernetCardDistributedVirtualPortBackingInfo _nic_backing = (VirtualEthernetCardDistributedVirtualPortBackingInfo)_workloadnic.Backing;
network_moid = _nic_backing.Port.SwitchUuid
vds = true;
}
catch (Exception ex) { }
}
if (!vswitch || !vds)
{
Logger.log(String.Format("UpdateVMwareWorkload: Could not determine workload network backing type"), Logger.Severity.Error);
}
答案 0 :(得分:0)
它认为这样可以正常工作......
if (_workloadnic.Backing is VirtualEthernetCardNetworkBackingInfo)
{
}
else if (_workloadnic.Backing is VirtualEthernetCardDistributedVirtualPortBackingInfo)
{
}