问题描述
我试图让一个目录服务对象的uSNChanged值的Int64值。不幸的是,它总是回来为某种一个COM对象。我用铸造的Int64试过了,叫Int64.Parse(),并调用Convert.ToInt64()。所有这些工作。
对于一个给定的DirectoryEntry对象,code将显示属性:
私有静态无效DisplaySelectedProperties(的DirectoryEntry objADObject)
{
尝试
{
字符串[]属性=新的String [] {
显示名称,
whenCreated
whenChanged
uSNCreated
uSNChanged
};
Console.WriteLine(的String.Format(显示{0}中选择属性,objADObject.Path));
的foreach(在属性字符串strAttrName)
{
的foreach(在objADObject.Properties VAR objAttrValue [strAttrName])
{
字符串strAttrValue = objAttrValue.ToString();
Console.WriteLine(的String.Format({0,-22}:{1},strAttrName,strAttrValue));
}
}
Console.WriteLine();
}
赶上(例外前)
{
抛出新ApplicationException的(的String.Format(致命错误访问:{0} - {1},objADObject.Path,ex.Message),前);
}
}
这是输出:
显示LDAP中选择属性://服务器/ o =组织/ CN = OBJ 显示名:显示名称 whenCreated:7/8/2009下午7点29分02秒 whenChanged:7/8/2009下午10时42分二十三秒 uSNCreated:系统.__ ComObject uSNChanged:系统.__ ComObject
我如何将其转换系统.__ ComObject成的Int64?
解决方案中使用:
这是我使用了基于marc_s的解决方案如下解决方案:
公共静态的Int64 ConvertADSLargeIntegerToInt64(对象adsLargeInteger)
{
。VAR highPart =(Int32)已adsLargeInteger.GetType()InvokeMember(HighPart,System.Reflection.BindingFlags.GetProperty,空,adsLargeInteger,NULL);
。VAR lowPart =(Int32)已adsLargeInteger.GetType()InvokeMember(LowPart,System.Reflection.BindingFlags.GetProperty,空,adsLargeInteger,NULL);
返回highPart *((的Int64)UInt32.MaxValue + 1)+ lowPart;
}
我用这个片段的code在我ADSI浏览器的的 的是C#编写的:
的Int64 iLargeInt = 0;
IADsLargeInteger int64Val =(IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;
据我所知,这应该只是罚款。
马克·
I'm trying to get the Int64 value of a Directory Services object's "uSNChanged" value. Unfortunately, it is always coming back as a COM object of some kind. I've tried using casting to Int64, calling Int64.Parse(), and calling Convert.ToInt64(). None of these work.
For a given DirectoryEntry object, this code will display the properties:
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);
}
}
This is the output:
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
How do I convert that System.__ComObject into a Int64?
Solution Used:
This is the solution I used based on marc_s's solution below:
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;
}
I'm using this snippet of code in my ADSI Browser BeaverTail which is written in C#:
Int64 iLargeInt = 0;
IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;
As far as I can tell, this should work just fine.
Marc
这篇关于如何改变System.DirectoryEntry" uSNChanged"属性值为Int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!