我想获得LastLogonTimestamp
通过调用从Active Directory
Principal.ExtensionGet("lastLogonTimestamp")
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
这将返回的数组Object
(即Object()
或null
。 麻烦的是它抱怨我投来Long
(或其他类型的我已经试过这样的: ULong
, Date
和String
)。 它总是告诉我这样的事情:
从类型“_ComObject”转换为类型“龙”是无效的。
在一个新的问题 ,我阐述了走另一条路(从日期时间到64位)
使用中提供的C#代码的链接通过以下HansPassant的评论,我解决了这个用下面的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
和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;
}
}
文章来源: How do I convert type _ComObject to a native type like Long or other (getting cast error)?