EnterpriseLibrary Validation的配置导致AmbiguousMatchException问题

1 篇文章 0 订阅
前几天在用EL的Validation进行App.Config配置时遇到了比较头疼的情况: 在对隐藏父类成员(一般为属性)的子类成员进行配置时一切正常,但是在运行后就抛出了

System.Reflection.AmbiguousMatchException异常,查了EL的源码,发现源头出在了ValidationReflectionHelper类中的GetProperty方法中,如下:

internal static PropertyInfo GetProperty(Type type, string propertyName, bool throwIfInvalid)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            PropertyInfo propertyInfo = type.GetProperty(propertyName,BindingFlags.Public | BindingFlags.Instance);
         
            ...//省略
            }

问题就出在红色标记处,因为在反射调用时父类和子类各有一个同名的属性,在运行时就会抛出匹配的异常,解决这个问题就只能改源码了:

...
 internal static PropertyInfo GetProperty(Type type, string propertyName, bool throwIfInvalid)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            PropertyInfo propertyInfo = null;
            try
            {
                propertyInfo = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
            }
            catch (AmbiguousMatchException )
            {
                propertyInfo = type.GetProperty(propertyName, BindingFlags.DeclaredOnly |  BindingFlags.Public | BindingFlags.Instance);
            }
         
           ...

            return propertyInfo;
        }
...

在获取属性时仅考虑子类的成员即可。改完源码后需要对原程序集进行重新编译,可能还得添加强命名,并且对App配置文件中关于Validation段的设置都要进行修改,重新运行后问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值