我定义了一组属性,这些值都存储为字符串。 我需要返回一组新属性,其中每个新属性的值可以通过3种方式之一派生。
我解决这个问题的方法是创建一个定义GetValue(Dictionary SourceValues)方法的IMappable接口。
对于每个结果,我使用此方法中所需的逻辑定义了此接口的实现。
我有一个工厂方法,它根据属性名称返回一个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);
}
}
这是解决此问题的最佳方式吗?
感觉就像是最受欢迎的,但我担心动态类加载的性能损失。
答案 0 :(得分:0)
慢吗?对其进行测试,然后担心它的速度有多慢。