我有下面的基類和派生類。有2點,我覺得我的代碼有問題:c#避免拳擊和重複代碼
public class FieldCollection
{
public FieldCollection()
{
Fields = new List<FieldBase>();
}
public List<FieldBase> Fields { get; set; }
public void InitFieldList()
{
foreach (var item in DbFieldList)
{
if (item.Type == FieldTypes.int)
{
Fields.Add(new Field<int>());
}
else
if (item.Type == FieldTypes.string)
{
Fields.Add(new Field<string>());
}
...
};
}
public void SetFieldValue(string fieldName, object value)
{
FieldBase field = FindField(fieldName);
if ((field as FInt) != null)
{
(field as FInt).SetValue(Convert.ToInt32(value));
}
else
if ((field as FString) != null)
{
(field as FString).SetValue(Convert.ToString(value));
}
else
if ((field as FDate) != null)
{
(field as FDate).SetValue(Convert.ToDateTime(value));
};
}
public FieldBase FindField(string fieldName)
{
FieldBase field = Fields.FirstOrDefault(f => (String.Equals(f.Name, fieldName, StringComparison.CurrentCultureIgnoreCase)));
return field;
}
}
public abstract class FieldBase
{
public string Name { get; set; }
}
public class FInt : FieldBase
{
public void SetValue(int value)
{
}
}
public class FString : FieldBase
{
public void SetValue(string value)
{
}
}
public class FDate : FieldBase
{
public void SetValue(DateTime value)
{
}
}
- 重複:如果未來出現了一個新的派生類,我將有額外的if語句等補充。
if ((field as FInt) != null) { (field as FInt).SetValue(Convert.ToInt32(value)); } else if ((field as FString) != null) { (field as FString).SetValue(Convert.ToString(value)); } else if ((field as FDate) != null) { (field as FDate).SetValue(Convert.ToDateTime(value)); };
- 裝箱和取消裝箱:我被迫對象值轉換爲基於派生的類的類型的期望類型:
(field as FInt).SetValue(Convert.ToInt32(value)); (field as FString).SetValue(Convert.ToString(value)); (field as FDate).SetValue(Convert.ToDateTime(value));
我對如何設計這些類以克服上述提及感興趣編輯問題。
您的幫助非常感謝。
看起來你需要仿製藥 –