[英]Mapping a set of properties to another set where multiple source properties may be used in conversion rules?
我定義了一組屬性,所有值都存儲為字符串。 我需要返回一組新的屬性,其中每個新屬性的值都可以通過3種方式之一導出。
解決此問題的方法是創建一個IMappable接口,該接口定義方法GetValue(Dictionary SourceValues)。
對於每個結果,我都使用此方法中所需的邏輯定義了此接口的實現。
我有一個工廠方法,該方法根據屬性名稱返回IMappable:
private IMappable GetMapper(string LocalPropertyName)
{
char[] Chars = LocalPropertyName.ToCharArray();
Chars[0] = Char.ToUpper(Chars[0]);
string ClassName = new string(Chars) + "Mapping";
try
{
Assembly AssemblyInstance = Assembly.GetExecutingAssembly();
Type ClassType = AssemblyInstance.GetType("MyNamespace.Rules." + ClassName, false, true);
return (IMappable) System.Activator.CreateInstance(ClassType);
}
catch (System.Exception e)
{
//No Mapper exists
return new DefaultMapping(LocalPropertyName);
}
}
這是解決此問題的最佳方法嗎?
感覺像是最聰明的人,但我擔心動態類加載對性能的影響。
慢嗎? 對其進行分析,然后擔心它有多慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.