从具有已知属性的对象分配值

时间:2016-05-09 07:01:12

标签: c# properties active-directory dynamic-properties

我正在用巫婆写一个课程,创建了员工手机的概述。 我收到包含手机作为孩子的Activesync对象的信息。

这是我目前的代码。如果孩子不包含任何空值,它就有效。

foreach (DirectoryEntry child in directoryObject.Children)
            {
                var activeSyncPhone = new ActiveSync.Phone();

                activeSyncPhone.Cn = child.Properties["cn"].Value.ToString(); //string
                activeSyncPhone.DistinguishedName = child.Properties["distinguishedName"].Value.ToString(); //sting
                activeSyncPhone.InstanceType = (int)child.Properties["instanceType"].Value; //int
                activeSyncPhone.WhenCreated = (DateTime)child.Properties["whenCreated"].Value; //datetime
                activeSyncPhone.WhenChanged = (DateTime)child.Properties["whenChanged"].Value; //datetime
                activeSyncPhone.Name = child.Properties["name"].Value.ToString(); //string
                activeSyncPhone.ObjectCategory = child.Properties["objectCategory"].Value.ToString(); //string
                activeSyncPhone.MsExchFirstSyncTime = (DateTime)child.Properties["msExchFirstSyncTime"].Value;//datetime
                activeSyncPhone.MsExchDeviceEASVersion = child.Properties["msExchDeviceEASVersion"].Value.ToString();//string
                activeSyncPhone.MsExchDeviceFriendlyName = child.Properties["msExchDeviceFriendlyName"].Value.ToString(); //string
                activeSyncPhone.MsExchDeviceAccessState = (ActiveSync.Phone.DeviceAccessState)child.Properties["msExchDeviceAccessState"].Value; //int
                activeSyncPhone.MsExchDeviceID = child.Properties["msExchDeviceID"].Value.ToString(); //string
                activeSyncPhone.MsExchDeviceType = child.Properties["msExchDeviceType"].Value.ToString(); //string
                try
                {
                    activeSyncPhone.MsExchDeviceIMEI = child.Properties["msExchDeviceIMEI"]?.Value.ToString(); //string
                }
                catch
                {
                    activeSyncPhone.MsExchDeviceIMEI = "Could not find IMEI";
                }

                activeSyncPhone.MsExchDeviceUserAgent = child.Properties["msExchDeviceUserAgent"].Value.ToString(); //string
                activeSyncPhone.MsExchVersion = child.Properties["msExchVersion"].Value.ToString(); //string
                activeSyncPhone.MsExchDeviceAccessStateReason = (ActiveSync.Phone.DeviceAccessStateReason)child.Properties["msExchDeviceAccessStateReason"].Value; //string
                activeSyncPhone.MsExchUserDisplayName = child.Properties["msExchUserDisplayName"].Value.ToString(); //string
                activeSyncPhone.MsExchDeviceModel = child.Properties["msExchDeviceModel"].Value.ToString(); //string
                activeSyncPhone.MsExchDeviceOS = child.Properties["msExchDeviceOS"].Value.ToString(); //string
                activeSyncPhone.ObjectGUID = child.Properties["objectGUID"].Value.ToString(); //string
                activeSyncUnits.PhoneList.Add(activeSyncPhone);


                child.Close();
            }
            directoryObject.Close();

我想知道是否有任何方法可以让它更加健壮。我正在研究动态设置ActiveSyncPhone的属性,然后使用列表设置所有属性。但是C#是一种强类型语言,我认为id利用了这方面的类型安全性和性能优势。

我认为用if语句检查每个child.property for null可能有更好的方法吗?并且还有一个更好的方法来获得孩子的财产?

1 个答案:

答案 0 :(得分:1)

你可以为它创建一个通用函数:

// you'll have to figure out the type of the `child.Properties`
public static T GetValue<T>(TypeOfChildProperties properties, string name, T defaultValue = default(T))
{
    var value = properties[name];

    if (value == null)
        return defaultValue;

    return (T)value;

    // if you have some cast problems, you could use this:
    return (T)Convert.ChangeType(value, typeof(T));
}
activeSyncPhone.Cn = GetValue<string>(child.Properties, "cn");
activeSyncPhone.DistinguishedName = GetValue<string>(child.Properties, "distinguishedName");
activeSyncPhone.InstanceType =  GetValue<int>(child.Properties, "instanceType");
activeSyncPhone.MsExchDeviceIMEI = GetValue<string>(child.Properties, "msExchDeviceIMEI", "Could not find IMEI");