Nie*_*ein 2 c# ienumerable casting
我想将一个对象取消装入IEnumerable.我检查是否可以为对象分配IEnumerable然后如果是,我想循环遍历对象中的值.但是,当我执行以下操作时:
if (typeof(IEnumerable<IRecord>).IsAssignableFrom(propertyValue.GetType()))
{
foreach (var property in IEnumerable<IRecord>(propertyValue))
{
var test = property;
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如何将propertyValue指定为IEnumerable?
你要:
if (typeof(IEnumerable<IRecord>).IsAssignableFrom(propertyValue.GetType()))
{
foreach (var property in (IEnumerable<IRecord>)propertyValue)
{
var test = property;
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
var enumerable = propertyValue as IEnumerable<IRecord>;
if (enumerable != null)
{
foreach (var property in enumerable)
{
var test = property;
}
}
Run Code Online (Sandbox Code Playgroud)