Kentico RefreshSecurityParams函数中的代码会遇到错误Object must implement IConvertible.
System.InvalidCastException: Object must implement IConvertible.
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at CMS.DataEngine.SimpleDataClass.SetData(Int32 columnIndex, Object value)
at CMS.DataEngine.SimpleDataClass.LoadData(IDataContainer data, Boolean loadNullValues)
at CMS.DataEngine.AbstractInfoBase`1.LoadData(LoadDataSettings settings)
at CMS.DataEngine.AbstractInfo`1.New(LoadDataSettings settings)
at CMS.DataEngine.AbstractInfo`1.NewObject(LoadDataSettings settings)
at CMS.DataEngine.ModuleManager.GetObject(LoadDataSettings settings)
at CMS.DataEngine.ModuleManager.GetObject(DataRow objectRow, String objectType, Boolean throwIfNotFound)
at CMS.DataEngine.InfoObjectCollection`1.CreateNewObject(DataRow dr)
at CMS.DataEngine.InfoObjectCollection`1.GetItemInternal(Int32 index)
at CMS.DataEngine.InfoObjectCollection`1.GetItem(Int32 index)
at CMS.DataEngine.InfoObjectCollection`1.get_InternalCount()
at CMS.DataEngine.InfoObjectCollection`1.<GetEnumeratorInternal>d__234.MoveNext()
at CMS.DataEngine.InfoObjectCollection`1.<GetEnumerator>d__233.MoveNext()
at UpgradeProcedure.RefreshSecurityParams()
private static void LoadCMSUser() { string str = "cms.user"; InfoObjectCollection infos = new InfoObjectCollection(str); foreach (var info in infos) { Console.WriteLine(); } }
这段代码,直接进行源码调试。
info.Generalized的类型是CMS.DataEngine.GeneralizedAbstractInfo`1[CMS.Membership.UserInfo]
info.Generalized.MainObject.GetType().ToString() 类型是CMS.Membership.UserInfo
问题出在转换数据上
Why doesn't Convert.ChangeType(string) work with DateTimeOffset, when it works with DateTime?
If you look at the documentation for the Convert.ChangeType
method, you'll see the following note:
Exceptions
InvalidCastException
This conversion is not supported.
-or-
value
isnull
andconversionType
is a value type.
-or-
value
does not implement the IConvertible interface.
Since we know that value
is not null and that string
implements the IConvertible
interface, then the only reason left is the first one:
This conversion is not supported.
What we can do is first convert it to a DateTime
, and then convert that to a DateTimeOffset
. There is some documentation here that describes various methods to accomplish this, one of which is:
"You can also create a new DateTimeOffset value by assigning it a DateTime value"
So we can just do:
DateTimeOffset dateTimeOffset = (DateTime) Convert.ChangeType(dateString, typeof(DateTime));
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-07-09 Serilog.Sinks.Elasticsearch
2020-07-09 serilog Getting Started
2019-07-09 __doPostBack function
2019-07-09 ASP.NET postback with JavaScript (UseSubmitBehavior)
2019-07-09 ClientScriptManager 和 ScriptManager RegisterClientScriptBlock
2019-07-09 sonarqube修改自己的图像avatar
2015-07-09 C#中的异常处理