我使用反射来更新对它们进行了更新并保存到 mongodb 的对象
I use reflection to update objects which have had updates made to them and saved to mongodb
private void updateSelf(MongoDoc newDoc) { Type type = this.GetType(); foreach (var i in type.GetProperties()) { if (i.GetCustomAttributes(false).Any(x => x is MongoDB.Bson.Serialization.Attributes.BsonIgnoreAttribute)) continue; Object oldValue = i.GetValue(this, null); Object newValue = i.GetValue(newDoc, null); if (!Object.Equals(oldValue, newValue) && !((oldValue == null) && (newValue == null))) { i.SetValue(this, newValue, null); } } }这在大部分情况下都有效,但是 i.SetValue(this, newValue, null); 在尝试更新此属性时抛出异常:
this is working for the most part but the i.SetValue(this, newValue, null); throws an exception when trying to update this property:
public uint Revision { get; private set; }这是试图更新 Product 类型的对象,它是 MongoDoc 的派生类型,其中包含属性 public uint Revision { get;私人订制;} 导致异常 Property set Method not found 我不确定是什么导致了这个,因为它适用于我的所有其他属性,只是这个抛出和异常.非常感谢任何帮助
this is trying to update an object of type Product which is a derived type of MongoDoc which contains the property public uint Revision { get; private set; } which is causing the exception Property set Method not found I'm not sure what is causing this because it works on all my other properties, just this one throws and exception. Any help much appreciated
更新:
我已经尝试了以下答案:
I have tried the answer below:
i.SetValue(this, newValue, System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic, null, null, null);但不幸的是完全相同的结果,它仍然在 Revision 属性上抛出异常.
but unfortunately the exact same result, it still throws the exception on the Revision property.
更新:
异常:
System.ArgumentException was unhandled Message=Property set method not found. Source=mscorlib StackTrace: at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) at Flo.Client.Docs.MongoDoc.updateSelf(MongoDoc newDoc) in F:\Flo\Flo.Client\Docs\MongoDoc.cs:line 162 at Flo.Client.Docs.MongoDoc.UpdateToMongo(MongoDoc newDoc) in F:\Flo\Flo.Client\Docs\MongoDoc.cs:line 120 at Flo.Client.Docs.Product.EditProduct(String Name, Nullable`1 State) in F:\Flo\Flo.Client\Docs\Product.cs:line 89 at Flo.Client.Program.Main() in F:\Flo\Flo.Client\Program.cs:line 26 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: 推荐答案我用这个解决了这个问题,感谢 Dylan Meador 给我指出另一个问题,让我得到了足够的解决方案:
I fixed it with this, thanks to Dylan Meador for pointing me to another question which gave me enough to get the solution:
private void updateSelf(MongoDoc newDoc, Type type) { foreach (var i in type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)) { if (i.GetCustomAttributes(false).Any(x => x is MongoDB.Bson.Serialization.Attributes.BsonIgnoreAttribute)) continue; Object oldValue = i.GetValue(this, null); Object newValue = i.GetValue(newDoc, null); if (!Object.Equals(oldValue, newValue) && !((oldValue == null) && (newValue == null))) { i.SetValue(this, newValue, null); } } Type baseType = type.BaseType; if (baseType != null) { this.updateSelf(newDoc, baseType); } }看起来需要将 Type 显式设置为基类类型,以便对该特定属性使用 set 访问器.
It looks like the Type needed to be explicitly set to the base class type in order to use the set accessor for that particular property.
更多推荐
C# 反射 SetValue() 找不到设置访问器
发布评论