【问题标题】:.net core C# use dynamic property name on EF Core Database first generated model class.net core C# 在 EF Core 数据库上使用动态属性名称首先生成的模型类
【发布时间】:2019-09-15 19:42:00
【问题描述】:

我有一个用户详细信息类

public partial class UserDetails
    {
        public int? Level { get; set; }
        public string Unit { get; set; }
        public string Bio { get; set; }
        public bool? Gender { get; set; }
        public int? Mobile { get; set; }
        public string Photo { get; set; }
    }

我正在写一个更新方法:

public bool UpdateDetails(string userId, UserProperties updateProperty, string value)
        {
         switch(updateProperty)
            {
                case UserProperties.Unit:
                    details.Unit = value;
                    break;
                case UserProperties.Photo:
                    details.Photo = value;
                    break;
                default:
                    throw new Exception("Unknown User Detail property");
            }

我可以在 JavaScript 中做一些类似动态属性的事情吗? 例如

var details = new UserDetails();
details["Unit"] = value;

更新

截至 2019 年!试试这个新功能怎么样?! 动态对象 DynamicObject.TrySetMember(SetMemberBinder, Object) Method

我在想怎么写。

【问题讨论】:

标签: c# .net-core entity-framework-core


【解决方案1】:

您可以通过反射对象上存在的属性来做到这一点。

C# 有一个名为Indexers 的功能。您可以像这样扩展您的代码以实现您所期望的行为。

 public partial class UserDetails
    {
        public int? Level { get; set; }
        public string Unit { get; set; }
        public string Bio { get; set; }
        public bool? Gender { get; set; }
        public int? Mobile { get; set; }
        public string Photo { get; set; }
         // Define the indexer to allow client code to use [] notation.
       public object this[string propertyName]
       {
          get { 
            PropertyInfo prop = this.GetType().GetProperty(propertyName);
            return prop.GetValue(this); 
          }
          set { 
            PropertyInfo prop = this.GetType().GetProperty(propertyName);
            prop.SetValue(this, value); 
          }
       }
    }

除此之外,如果你不知道运行时的属性,你可以使用dynamic类型。

【讨论】:

  • 嗨 Alen,感谢您的这个想法,我在考虑 Indexer,但它只是将我在 Switch 中编写的代码移动到了 Indexer,这是我想要避免的。请参阅我的新功能更新:DynamicObject.TrySetMember()
  • 仅供参考,使用 FromSQLRaw 方法时,这对我不起作用。属性永远不会出现在类中,对象的任何属性总是无效的。
【解决方案2】:

如果您不想使用反射,您可以稍微调整 Alens 解决方案以使用字典来存储数据。

public class UserDetails
{
    private Dictionary<string, object> Items { get; } = new Dictionary<string, object>();

    public object this[string propertyName]
    {
        get => Items.TryGetValue(propertyName, out object obj) ? obj : null;
        set => Items[propertyName] = value;
    }

    public int? Level
    {
        get => (int?)this["Level"];
        set => this["Level"] = value;
    }
}

【讨论】:

  • 或者如果继承 Dictionary 类,可以扩展 UserDetails 类,以消除添加的冗余索引器“this”。
  • @DanielC 好主意 :)
  • 您好,就我而言,我不需要 Getter,因为我的模型类是从实体框架生成的。我需要做的就是更新那些生成的模型类的特定属性。
【解决方案3】:

最接近的是 ExpandoObject:

https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=netframework-4.8

例如:

dynamic sampleObject = new ExpandoObject();
sampleObject.test = "Dynamic Property";
Console.WriteLine(sampleObject.test);

【讨论】:

  • 嗨 Metheny,谢谢你的想法,我实际上已经检查过了,它用于动态构建一个对象,这不是我的情况,因为我的 Model 类是由 EF 生成的,无法更改.
猜你喜欢
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多