有没有办法做到这一点?我尝试测试对象的属性是否存在,如果存在,我想为它设置一个值.(也许完整的想法很糟糕,如果是真的 - 为什么?)
class Info
{
public string X1{ set; get; }
public string X2{ set; get; }
public string X3{ set; get; }
}
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("X1","blah1");
values.Add("X2","blah2");
values.Add("NotThere","blah3");
Info info = new Info();
foreach (var item in values)
{
string propertyName = item.Key;
string value = item.Value;
if (info.GetType().GetProperty(propertyName) != null) //this probably works
{
info.propertyName = value; //this doesn't, how to set it?
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法用defaultValue发送属性但是用数据库计算结果更新它?数据库中有一个触发器,对于输入-1,触发一个计算新值的过程.如何获得价值?
<Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="None" DefaultValue="-1" />
var x = new Stuff();
db.Stuff.AddObject(x);
db.SaveChanges();
return x.ID //gives -1, but the computed value should be different.
Run Code Online (Sandbox Code Playgroud)