企业库-从ParameterValue表达式获取值 [英] Enterprise Library - Get value from ParameterValue Expression

查看:248
本文介绍了企业库-从ParameterValue表达式获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将企业库TypeRegistration ConstructorParameters转换为键/值对(通常是HashTable或IDictionary)的集合。

I am trying to convert Enterprise Library TypeRegistration ConstructorParameters to a collection of key/value pair (a HashTable or an IDictionary in general).

ConstructorParameters属性是一个IEnumerableOf(ParameterValue),所以我的问题是如何从每个ParameterValue对象中提取值。

The ConstructorParameters property is an IEnumerableOf(ParameterValue) so my problem is how to extract the values from each ParameterValue object.

每个ParameterValue对象都包含一个类型和一个表达式。

Every ParameterValue object contains a Type and an Expression.

例如如果ParameterValue包含: EventLoggingEnabled = false
,那么我可以使用expression.Member.Name
获得键(即EventLoggingEnabled),但是我找不到方法来获取值(是假)。

For ex. if a ParameterValue contains: "EventLoggingEnabled = false" then I can get the key (which is the EventLoggingEnabled) using expression.Member.Name but I can't find a way to get the value (which is "false").

有什么想法吗?

推荐答案

有您看过UnityContainerConfigurator的实现吗?即使您不想使用Unity,也可以看到那里如何处理类型注册内容并适应Windsor API。

Have you looked at the implementation of the UnityContainerConfigurator? Even if you don't want to use Unity, you can see how the type registration stuff is handled there and adapt to the Windsor API.

您通常不需要编码到原始ParameterValue类,并通过lambda表达式戳入。实际上有三个子类:

You generally don't need to code to the raw ParameterValue class and poke through the lambda expressions. There are actually three subclasses:


  • ConstantParameterValue

  • ContainerResolvedParameter

  • ContainerResolvedEnumerableParameter

ConstantParameterValue直接为.Value属性提供值。当容器需要注入参数的值时,将使用ContainerResolvedParameter;当需要注入集合时,将使用ContainerResolvedEnumerableParameter。每个ParameterValue实例实际上都是这些类型之一。

The ConstantParameterValue gives you the value directly as the .Value property. ContainerResolvedParameter is used when the value of the parameter needs to be injected by the container, and ContainerResolvedEnumerableParameter is used when you have a collection that needs to be injected. Every instance of ParameterValue is actually one of these types.

因此,您应该做的是尝试将其强制转换为每个类型,然后根据实际类型进行切换。有一个实用程序基类ParameterValueVisitor,它使您可以在ParameterValues上实现Visitor模式,以使代码更整洁。

So, what you should do is try to cast to each one, and then switch based on the actual type. There's a utility base class, ParameterValueVisitor, that lets you implement the Visitor pattern over ParameterValues to make your code cleaner.

因此,我要做的就是取消选择lambdas-您不需要这样做。实施访问者以使用基类提取您所需的信息,然后将在具体的类中为您提供预先消化的信息。查看UnityContainerConfigurator,以获取有关此操作的示例。

So, what I'd do is drop the picking through lambdas - you don't need to do it. Implement a visitor to pull out the information you need using the base class, then the pre-digested information will be available to you in the concrete classes. Look at the UnityContainerConfigurator for an example of how this is done.

这篇关于企业库-从ParameterValue表达式获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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