我如何传递一个属性作为代表? [英] How can I pass a property as a delegate?

查看:133
本文介绍了我如何传递一个属性作为代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个理论性的问题,我已经有了一个解决我的问题,我花了另一条不同的道路,但我认为这个问题仍然是潜在的有趣。

This is a theoretical question, I've already got a solution to my problem that took me down a different path, but I think the question is still potentially interesting.

我可以以同样的方式,我可以用的方法传递对象属性的代表?例如:

Can I pass object properties as delegates in the same way I can with methods? For instance:

比方说,我有装了数据的数据读取,并且每个字段的值需要传递到不同类型的已检查的DBNull性能。如果试图得到一个单一的领域,我可能会写这样的:

Let's say I've got a data reader loaded up with data, and each field's value needs to be passed into properties of differing types having been checked for DBNull. If attempting to get a single field, I might write something like:

if(!rdr["field1"].Equals(DBNull.Value)) myClass.Property1 = rdr["field1"];

但是,如果我有说100场,这变得笨拙非常快。有一对夫妇的方式调用做到这一点可能看起来不错:

But if I've got say 100 fields, that becomes unwieldy very quickly. There's a couple of ways that a call to do this might look nice:

myClass.Property = GetDefaultOrValue<string>(rdr["field1"]); //Which incidentally is the route I took

这也可能看起来不错,作为一个扩展方法:

Which might also look nice as an extension method:

myClass.Property = rdr["field1"].GetDefaultOrValue<string>();

或者

SetPropertyFromDbValue<string>(myClass.Property1, rdr["field1"]); //Which is the one that I'm interested in on this theoretical level

在第二种情况下,该属性将需要以将其设置为传递作为代表。

In the second instance, the property would need to be passed as a delegate in order to set it.

所以,问题是两部分:

  1. 这可能吗?
  2. 在什么样子?

[由于这只是理论上的,在VB或C#的答案都可以接受我]

[As this is only theoretical, answers in VB or C# are equally acceptable to me]

编辑:有一些华而不实的答案在这里。感谢所有。

There's some slick answers here. Thanks all.

推荐答案

(添加第二个答案,因为它是在一个完全不同的方法)

(Adding a second answer because it's on a completely different approach)

要解决原来的问题,这更多的是在一个DataReader想要一个漂亮的API映射名为值的属性在你的对象,可以考虑 System.ComponentModel.TypeDescriptor - 一个经常被忽视的替代做反射dirtywork自己。

To address your original problem, which is more about wanting a nice API for mapping named values in a datareader to properties on your object, consider System.ComponentModel.TypeDescriptor - an often overlooked alternative to doing reflective dirtywork yourself.

下面是一个有用的片段:

Here's a useful snippet:

var properties = TypeDescriptor.GetProperties(myObject)
    .Cast<PropertyDescriptor>()
    .ToDictionary(pr => pr.Name);

这是为您创建对象的propertydescriptors的字典。

That creates a dictionary of the propertydescriptors of your object.

现在我可以做到这一点:

Now I can do this:

properties["Property1"].SetValue(myObject, rdr["item1"]);

的PropertyDescriptor 的setValue方法(不像 System.Reflection.PropertyInfo 的等价物)会做类型转换为你 - 解析字符串为整数,等等

PropertyDescriptor's SetValue method (unlike System.Reflection.PropertyInfo's equivalent) will do type conversion for you - parse strings as ints, and so on.

这个是可以想见的属性驱动的方法来迭代通过属性集合(的PropertyDescriptor 有一个属性属性,让你得到加到属性)搞清楚要使用的值在DataReader的任何自定义属性;或具有接收因propertyName字典的方法 - 的ColumnName的映射,遍历和执行所有这些为您设置

What's useful about this is one can imagine an attribute-driven approach to iterating through that properties collection (PropertyDescriptor has an Attributes property to allow you to get any custom attributes that were added to the property) figuring out which value in the datareader to use; or having a method that receives a dictionary of propertyname - columnname mappings which iterates through and performs all those sets for you.

我怀疑这样的方法可能会给你你需要的方式API的快捷方式的λ-EX pression反光挂羊头卖狗肉 - 在这种情况下 - 不会

I suspect an approach like this may give you the API shortcut you need in a way that lambda-expression reflective trickery - in this case - won't.

这篇关于我如何传递一个属性作为代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆