如何将System.DirectoryEntry"uSNChanged"属性值更改为Int64

Sim*_*bee 6 directoryservices active-directory directoryentry int64

我正在尝试获取目录服务对象的"uSNChanged"值的Int64值.不幸的是,它总是以某种COM对象的形式回归.我尝试使用转换为Int64,调用Int64.Parse(),并调用Convert.ToInt64().这些都不起作用.

对于给定的DirectoryEntry对象,此代码将显示以下属性:

    private static void DisplaySelectedProperties(DirectoryEntry objADObject)
    {
        try
        {
            string[] properties = new string[] {
                "displayName",
                "whenCreated",
                "whenChanged",
                "uSNCreated",
                "uSNChanged",
            };

            Console.WriteLine(String.Format("Displaying selected properties of {0}", objADObject.Path));
            foreach (string strAttrName in properties)
            {
                foreach (var objAttrValue in objADObject.Properties[strAttrName])
                {
                    string strAttrValue = objAttrValue.ToString();
                    Console.WriteLine(String.Format("   {0, -22} : {1}", strAttrName, strAttrValue));
                }
            }
            Console.WriteLine();
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format("Fatal error accessing: {0} - {1}", objADObject.Path, ex.Message), ex);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是输出:

Displaying selected properties of LDAP://server/o=org/cn=obj
   displayName            : Display Name
   whenCreated            : 7/8/2009 7:29:02 PM
   whenChanged            : 7/8/2009 10:42:23 PM
   uSNCreated             : System.__ComObject
   uSNChanged             : System.__ComObject

如何将该System .__ ComObject转换为Int64?


解决方案:

这是我根据marc_s的解决方案使用的解决方案:

    public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
    {
         var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         var lowPart  = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart",  System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
    }
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 5

我在我的ADSI浏览器BeaverTail中使用这段代码,它是用C#编写的:

Int64 iLargeInt = 0;

IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;
Run Code Online (Sandbox Code Playgroud)

据我所知,这应该工作得很好.