简体   繁体   English

如何将_ComObject类型转换为本机类型,例如Long或其他类型(出现强制转换错误)?

[英]How do I convert type _ComObject to a native type like Long or other (getting cast error)?

I am trying to get the LastLogonTimestamp from Active Directory by calling 我试图通过调用从Active Directory获取LastLogonTimestamp

Principal.ExtensionGet("lastLogonTimestamp")

VB.NET code: VB.NET代码:

<DirectoryProperty("lastLogonTimestamp")>
Public Property LastLogonTimestamp() As Date? ' no matter what this type is, I cannot cast the Object coming in
    Get
        Dim valueArray = ExtensionGet("lastLogonTimestamp")
        If valueArray Is Nothing OrElse valueArray.Length = 0 Then Return Nothing
        Return DateTime.FromFileTimeUtc(valueArray(0))
    End Get
    Set(value As Date?)
        ExtensionSet("lastLogonTimestamp", value)
    End Set
End Property

This returns an array of Object (ie Object() ) or null . 这将返回一个Object数组(即Object() )或null The trouble is it complains about my cast to Long (or other types I have tried like: ULong , Date , and String ). 麻烦在于它抱怨我对Long ULong (或其他我尝试过的类型: ULongDateString )。 It always tells me something like this: 它总是告诉我这样的事情:

Conversion from type '_ComObject' to type 'Long' is not valid. 从“ _ComObject”类型到“ Long”类型的转换无效。

In a new question , I set out to go the other way (from DateTime to 64 bit) 一个新问题中 ,我着手进行另一种选择(从DateTime到64位)

Using C# code provided in link via HansPassant's comment below, I resolved this with the following VB code: 使用下面通过HansPassant的注释在链接中提供的C#代码,我使用以下VB代码解决了此问题:

<DirectoryProperty("lastLogonTimestamp")>
Public Property LastLogonTimestamp() As Date?
    Get
        'Dim valueArray = GetProperty("whenChanged")
        Dim valueArray = ExtensionGet("lastLogonTimestamp") 'ExtensionGet("LastLogon")
        If valueArray Is Nothing OrElse valueArray.Length = 0 Then Return Nothing

        Dim lastLogonDate = valueArray(0)
        Dim lastLogonDateType = lastLogonDate.GetType()
        Dim highPart = CType(lastLogonDateType.InvokeMember("HighPart", Reflection.BindingFlags.GetProperty, Nothing, lastLogonDate, Nothing), Int32)
        Dim lowPart = CType(lastLogonDateType.InvokeMember("LowPart", Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Public, Nothing, lastLogonDate, Nothing), Int32)
        Dim longDate = CLng(highPart) << 32 Or (CLng(lowPart) And &HFFFFFFFFL)
        Dim result = IIf(longDate > 0, CType(DateTime.FromFileTime(longDate), DateTime?), Nothing)

        Return result
        'Return DateTime.FromFileTimeUtc(valueArray(0))
    End Get
    Set(value As Date?)
        ExtensionSet("lastLogonTimestamp", value)
    End Set
End Property

And the C# version (clipped from source ): 和C#版本(从剪切):

[DirectoryProperty("RealLastLogon")]
public DateTime? RealLastLogon
{
    get
    {
        if (ExtensionGet("LastLogon").Length > 0)
        {
            var lastLogonDate = ExtensionGet("LastLogon")[0];
            var lastLogonDateType = lastLogonDate.GetType();
            var highPart = (Int32)lastLogonDateType.InvokeMember("HighPart", BindingFlags.GetProperty, null, lastLogonDate, null);
            var lowPart = (Int32)lastLogonDateType.InvokeMember("LowPart", BindingFlags.GetProperty | BindingFlags.Public, null, lastLogonDate, null);

            var longDate = ((Int64)highPart << 32 | (UInt32)lowPart);

            return longDate > 0 ? (DateTime?) DateTime.FromFileTime(longDate) : null;
        }

        return null;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误无法将类型为System .__ ComObject的COM对象转换为类类型为System.String的对象 - Runtime error Unable to cast COM object of type 'System.__ComObject' to class type 'System.String' 如何在vb.net中将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ ESRI.ArcGIS.Carto.FeatureLayer”? - How to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Carto.FeatureLayer' in vb.net? 如何解决“无法将类型&#39;ASP.addtoroster_aspx&#39;的对象强制转换为&#39;System.Web.UI.WebControls.GridViewRow&#39;错误? - How do I resolve "Unable to cast object of type 'ASP.addtoroster_aspx' to type 'System.Web.UI.WebControls.GridViewRow' error? 无法将“System .__ ComObject”类型的COM对象转换为接口类型“Microsoft.Office.Interop.Excel.Worksheets” - Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Worksheets' 无法将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ mshtml.HTMLElementCollection” - Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.HTMLElementCollection 无法将类型为“ System .__ ComObject”的COM对象转换为类类型为“ System.Array”的对象 - Unable to cast COM object of type 'System.__ComObject' to class type 'System.Array' 如何动态获取 object 类型并转换为它? - How do I dynamically get an object type and cast to it? 如何将项目类型转换为共享类库类型 - How do I convert a project type to shared class library type 为什么我可以转换成一种而不是另一种? - Why I can convert to this one type and not the other? 将System .__ ComObject强制转换为已知类型的反射 - Casting System.__ComObject to known type reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM