【问题标题】:Can you access Scaffold attribute property of a class in C# and Entity Framework? [duplicate]您可以访问 C# 和实体框架中的类的 Scaffold 属性属性吗? [复制]
【发布时间】:2014-01-07 20:43:56
【问题描述】:

例如,如果你有

 public class user 
 {
    public string x { get; set; }

    [ScaffoldColumn(false)]
    public string y { get; set; }
 }

无论如何,当我查看实体以确定 y 是否确实有脚手架列为假时?我试过像这样以编程方式循环(伪代码):

foreach(var prop in user.GetProperties())
{  
    var attributes = prop.Attributes;
}

但似乎没有属性的属性属性或指示天气属性(在本例中为 y)是否为脚手架列。

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

您可以像这样获取所有具有[ScaffoldColumn(false)] 的属性:

var props = obj.GetType()
           .GetProperties(BindingFlags.Instance | BindingFlags.Public)
           .Select(p => new
           {
               Property = p,
               Attribute = p.GetCustomAttribute<ScaffoldColumnAttribute>()
           })
           .Where(p => p.Attribute != null && p.Attribute.Scaffold == false)
           .ToList();

【讨论】:

    【解决方案2】:

    在您的“用户”实现中,“x”和“y”不是属性。如下所述实现它们,它们将出现在“GetProperties”集合中

    public class user 
    {
        public string x { get; set; }
        [ScaffoldColumn(false)]
        public string y { get; set; }
    }
    

    【讨论】:

    • 他们是。抱歉,我使用伪代码来展示概念证明。我将更新我的问题以反映这一点。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多