我正在爲更改跟蹤開發通用反射類。我所做的所有課程的工作都很好。我準備將它作爲整個組的一部分工具移出。在我向所有人角色之前,我有興趣改進這一點。它從具有錯誤處理的方法中調用,以便部分不是問題。此外,在我們的邏輯中,這種方式完全符合我們爲變更追蹤而拼合對象的方式,但是我錯過了一些可能成爲問題的東西,即使它適用於通常的情況。通用更改跟蹤類改進
public class ChangeTracker
{
public static string GetChangesString<T,S>(T original, T current, S dto, string[] exluded)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] names = typeof(S).GetProperties();
string displayName = string.Empty;
foreach (PropertyInfo item in names)
{
if (exluded.Contains(item.Name)) continue;
//method that sets display name to either the property name or the display attribute if present
displayName = GetDisplayName(item);
object propA = original.GetType().GetProperty(item.Name).GetValue(original, null);
object propB = current.GetType().GetProperty(item.Name).GetValue(original, null);
if (propA == null && propB == null) continue;
if (propA == null && propB != null)
{
//appendline for value added
}
else if (propB == null && propA != null)
{
//appendline for value removed
}
else if (propA.ToString() != propB.ToString())
{
//appendline for value changed
}
}
return sb.ToString();
}
private static string GetDisplayName(PropertyInfo prop)
{
string display = string.Empty;
//Check for displayattribute and set correct name
return display;
}
}
具體這是我的問題。
有沒有更好的方法可以做propA和propB設置來提高性能?它可以在一個對象上進行更改,我已經測試了多達103個沒有性能問題的屬性,但是我儘可能避免這樣的東西。
感謝 吉米
我投票結束這個問題作爲題外話,因爲它應該張貼在http://codereview.stackexchange.com – 2015-02-07 20:22:01
授予第4點可能屬於codereview,但這個問題的原因實際上是針對SO的第1點和第2點。我不知道如何添加.Where刪除if語句,因爲它不適用於我嘗試過的所有內容。 – Jimmy 2015-02-07 20:40:22
'typeof(S).GetProperties()。Where(p =>!excluded.Contains(p.Name))';) - 你應該真的問每個問題一個問題,但(在適當的網站上)進入你有很多部分答案的情況。當你有4個不同的答案回答4個不同的問題時,你如何接受正確的答案? – 2015-02-07 20:44:28