Pregunta

Estoy tratando de obtener el valor Int64 del valor "uSNChanged" de un objeto de servicios de directorio. Por desgracia, siempre se va a volver como un objeto COM de algún tipo. He intentado usar el casting para Int64, llamando Int64.Parse (), y llamando Convert.ToInt64 (). Ninguno de estos trabajos.

Para un objeto DirectoryEntry dado, este código se mostrará las propiedades:

    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);
        }
    }

Esta es la salida:

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

¿Cómo convierto que System .__ ComObject en un Int64?


solución utilizada:

Esta es la solución I utilizarse en base a la solución de marc_s a continuación:

    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;
    }
¿Fue útil?

Solución

Estoy usando este fragmento de código en el navegador ADSI BEAVERTAIL que está escrito en C #:

Int64 iLargeInt = 0;

IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

Por lo que yo puedo decir, esto debería funcionar bien.

Marc

Otros consejos

Parece que se trata de una IADsLargeInteger tipo, por lo que será necesario un poco de magia de interoperabilidad para extraer los valores. este Tema contiene una implementación de ejemplo VB - y menciona problemas similares a su propio - sin embargo estoy muy lejos de poder verificar la utilidad de eso ahora. Espero que esto ayude.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top