调用PropertyInfo.GetValue时对参数计数不匹配异常进行故障排除

时间:2019-05-16 14:14:42

标签: c# reflection

我想知道是否有人可以指出我正确的方向。

这是导致参数计数不匹配错误的代码。
调用PropertyInfo.GetValue时,参数计数不匹配异常。我无法在我们的环境中实现它。在我们使用此代码的5个客户中,有一个客户生产环境就发生了。

 private string getValueFromType(object obj, string type, Type targetClassType, Type currentClassType = null)
        {
            //Reflection to find value for Jack Henry type mapped to specific OnBase KeywordType 
            if (obj == null)
            {
                return String.Empty;
            }
            var objType = obj.GetType();
            if (objType == targetClassType)
            {
                currentClassType = objType;
            }
            var properties = objType.GetProperties();
            var value = String.Empty;
            foreach (var property in properties)
            {
                var propValue = property.GetValue(obj, null);

                var elems = propValue as IList;
                if (elems != null)
                {
                    foreach (var item in elems)
                    {
                        value = this.getValueFromType(item, type, targetClassType, currentClassType);
                        if (value != String.Empty)
                        {
                            return value;
                        }
                    }
                }
                else
                {
                    if (property.PropertyType.Assembly.FullName.StartsWith("JackHenry"))
                    {
                        value = this.getValueFromType(propValue, type, targetClassType, currentClassType);
                        if (value != String.Empty)
                        {
                            return value;
                        }
                    }
                    else
                    {
                        if (currentClassType == targetClassType && objType.FullName == type && property.Name == "Value")
                        {
                            return propValue.ToString();
                        }
                    }
                }
            }
            return value;
        }

我已经对其进行了研究,一些帖子说这是因为对象类型具有索引属性,并且您在GetValue调用中将null传递给index参数。因此解决方案是

  var properties = objType.GetProperties().Where(p => p.GetIndexParameters().Length == 0);

但是我想理解为什么没有indexedparameters行的现有代码在任何时候都能工作。 有什么想法吗?

0 个答案:

没有答案
相关问题