【发布时间】: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
我在想怎么写。
【问题讨论】:
-
你可以通过反射来做到这一点:stackoverflow.com/a/1197004/3891038
-
反射可能会起作用,但我认为您不想使用它。
-
如果你已经硬编码了字符串,为什么不直接使用
details.Unit呢?
标签: c# .net-core entity-framework-core