How does InvokeMember know about the HighPart prop

2019-09-03 12:41发布

问题:

I want to use the System.Reflection library and not ActiveDs. I found this code on the web that parses the LargeInteger into HighPart and LowPart.
I don't understand it completely, particularly where is the method 'HighPart' and 'LowPart' defined? Is that within the Object class or do I have to define it?

Below is the code that parses the largeInteger:

de = new DirectoryEntry(curDomain,adUser,adPwd);        
object largeInteger = de.Properties["maxPwdAge"].Value;
System.Type type = largeInteger.GetType();
int high = (int)type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null);
int low = (int)type.InvokeMember("LowPart", BindingFlags.GetProperty, null, largeInteger, null);

Thanks!

回答1:

It's defined in the IADsLargeInteger, which is a COM interface.

http://msdn.microsoft.com/en-us/library/aa706037%28v=vs.85%29.aspx

To get rid of ActiveDs, you may defined the type yourself (C#):

[
ComImport,
InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
Guid("9068270B-0939-11D1-8BE1-00C04FD8D503")
]
public interface IADsLargeInteger
{
    int HighPart{get;set;}
    int LowPart{get;set;}
}

private long? GetLargeInt(DirectoryEntry de, string attrName)
{
    long? ret = null;

    IADsLargeInteger largeInt = de.Properties[attrName].Value as IADsLargeInteger;
    if (largeInt != null)
    {
        ret = (long)largeInt.HighPart << 32 | largeInt.LowPart;
    }

    return ret;
}