C# 反射 SetValue() 找不到设置访问器

编程入门 行业动态 更新时间:2024-10-11 05:28:44
本文介绍了C# 反射 SetValue() 找不到设置访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用反射来更新对它们进行了更新并保存到 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() 找不到设置访问器

本文发布于:2023-11-09 03:52:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   反射   SetValue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!