【发布时间】:2016-11-09 12:57:54
【问题描述】:
我有以下型号:
public class FormModel
{
public Guid {get; set;}
public Sections Sections {get; set}
}
[Flags]
public enum Sections
{
Test1= 0,
Test2= 1,
Test3= 2,
Test4= 4,
Test5= 8,
Test6= 16
}
我正在使用一个返回模型数据的服务:
var form = await _formService.GetById(formAnswer.FormId);
现在 Sections-property 包含:Test1 | Test2
我正在尝试像这样枚举这个属性:
var list = new List<string>();
foreach(var item in Enum.GetValues(typeof(form.Sections)))
{
//Add the form.Sections into the list.
}
但我得到了错误:
'form' 是一个变量,但用作类型
如何枚举模型的 Sections 属性并将值添加到列表中?
【问题讨论】:
-
类型为
typeof(Sections)。您将某些 Sections 值传递给typeof。 -
使用 form.Sections.GetType()
-
@CharlesMager:那么我应该怎么做 insead?
-
@Bryan 就像我说的 -
typeof(Sections). -
@CharlesMager:但如果我这样做,我会枚举整个枚举。不是我的服务结果中的 Sections-property..
标签: c# asp.net-mvc enums