我想将对象取消装入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?
答案 0 :(得分:5)
你想:
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;
}
}