2016-05-09 74 views
0

我正在用巫婆寫一個班級,以創建員工電話的概覽。 我正在將包含電話的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爲null嗎?還有更好的方式來獲得兒童財產?

回答

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"); 
+0

簡單而優雅的解決方案,喜歡它:)謝謝你這麼多。 – Sondre

相關問題