试图将对象取消框为IEnumerable,获取IEnumeraable是';type';而是像';变量
本文关键字:type 变量 IEnumeraable 获取 对象 取消 IEnumerable | 更新日期: 2023-09-27 18:29:42
我想将一个对象取消装箱到IEnumerable中。我检查对象是否可以分配一个IEnumerable,如果可以,我想循环遍历对象中的值。然而,当我做以下事情时:
if (typeof(IEnumerable<IRecord>).IsAssignableFrom(propertyValue.GetType()))
{
foreach (var property in IEnumerable<IRecord>(propertyValue))
{
var test = property;
}
}
IEnumerable给出以下错误:
Error 1 'System.Collections.Generic.IEnumerable<test.Database.IRecord>' is a 'type' but is used like a 'variable' D:'test.Test'ElectronicSignatureRepositoryTest.cs 397 46 test.Test
如何将propertyValue分配为IEnumerable?
您想要:
if (typeof(IEnumerable<IRecord>).IsAssignableFrom(propertyValue.GetType()))
{
foreach (var property in (IEnumerable<IRecord>)propertyValue)
{
var test = property;
}
}
你也可以做:
var enumerable = propertyValue as IEnumerable<IRecord>;
if (enumerable != null)
{
foreach (var property in enumerable)
{
var test = property;
}
}