Kentico RefreshSecurityParams函数中的代码会遇到错误Object must implement IConvertible.

 https://github.com/dotnet/runtime/blob/8377b0d938e1fecf0afda3d5a4f7f9bac3866bd5/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx#L2384

 

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 is null and conversionType 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    
posted @   ChuckLu  阅读(390)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.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#中的异常处理
点击右上角即可分享
微信分享提示