Cog*_*eel 12 c# reflection refactoring
我讨厌拥有一堆"左/右"方法.每次添加或删除属性时,我都必须修复每个方法.而代码本身看起来......错了.
public Foo(Foo other)
{
this.Bar = other.Bar;
this.Baz = other.Baz;
this.Lur = other.Lur;
this.Qux = other.Qux;
this.Xyzzy= other.Xyzzy;
}
Run Code Online (Sandbox Code Playgroud)
实际上,这只是一个展开的循环,它遍历属性,在对象之间复制它们.那么为什么不对这个事实说实话呢?反思救援!
public Foo(IFoo other)
{
foreach (var property in typeof(IFoo).GetProperties())
{
property.SetValue(this, property.GetValue(other, null), null);
}
}
Run Code Online (Sandbox Code Playgroud)
我可能只是试图强迫我从Lua学习到C#的范例,但这个特殊的例子对我来说似乎并不太臭.从这里开始,我开始做一些对字段顺序敏感的更复杂的事情.例如,我不是用一堆几乎相同的if语句来组成字段中的字符串,而是按照所需的顺序迭代它们:
public override string ToString()
{
var toJoin = new List<string>();
foreach (var property in tostringFields)
{
object value = property.GetValue(this, null);
if (value != null)
toJoin.Add(value.ToString());
}
return string.Join(" ", toJoin.ToArray());
}
private static readonly PropertyInfo[] tostringFields =
{
typeof(IFoo).GetProperty("Bar"),
typeof(IFoo).GetProperty("Baz"),
typeof(IFoo).GetProperty("Lur"),
typeof(IFoo).GetProperty("Qux"),
typeof(IFoo).GetProperty("Xyzzy"),
};
Run Code Online (Sandbox Code Playgroud)
所以现在我有了我想要的可迭代性,但我仍然有一堆代码镜像我感兴趣的每个属性(我也是为CompareTo做这个,使用不同的属性以不同的顺序).更糟糕的是失去了强烈的打字.这真的开始闻起来了.
那么在每个属性上使用属性来定义顺序呢?我开始走这条路,确实运作良好,但它让整个事情变得臃肿.它在语义上很有效,但我总是担心使用高级功能只是因为它们"整洁".以这种方式使用反射是否过度杀伤?是否还有其他解决左/右代码问题的解决方案?
使用反射本身并不坏,但是如果你以递归方式执行它,你会受到性能影响.
我不是硬编码复制构造函数的粉丝,因为开发人员在向类添加新属性时忘记更新它们.
还有其他方法可以实现您想要的功能,包括Marc Gravells 超级属性描述符,或者如果您想学习一些IL和OPCode,您可以使用System.Reflection.Emit甚至是Mono的Cecil.
以下是使用超级属性描述符的示例,您可以根据自己的需要进行定制:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Hyper.ComponentModel;
namespace Test {
class Person {
public int Id { get; set; }
public string Name { get; set; }
}
class Program {
static void Main() {
HyperTypeDescriptionProvider.Add(typeof(Person));
var properties = new Dictionary<string, object> { { "Id", 10 }, { "Name", "Fred Flintstone" } };
Person person = new Person();
DynamicUpdate(person, properties);
Console.WriteLine("Id: {0}; Name: {1}", person.Id, person.Name);
Console.ReadKey();
}
public static void DynamicUpdate<T>(T entity, Dictionary<string, object> {
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(typeof(T)))
if (properties.ContainsKey(propertyDescriptor.Name))
propertyDescriptor.SetValue(entity, properties[propertyDescriptor.Name]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您决定继续使用反射,则可以通过缓存对GetProperties()的调用来降低性能:
public Foo(IFoo other) {
foreach (var property in MyCacheProvider.GetProperties<IFoo>())
property.SetValue(this, property.GetValue(other, null), null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1214 次 |
| 最近记录: |