我希望我的标题可以更具描述性,但是我在理解下面的异常时遇到了麻烦。

我最近发生了这种情况,结果发现它是一个DependencyProperty,未正确声明,但是我发现没有此异常的帮助。

我唯一可以添加的另一件事是,它发生在调用GetHashCode方法时,该方法触发了通过反映对象的公共属性来派生哈希的方法。在这种情况下,该对象是从ViewModelBase类派生的,并且该异常被调用其名为Item的属性所击中,我猜这是IDataErrorInfo的isw组成部分的索引器,但这仅是一个猜测。

综上所述,我的直觉是,在输出任何有用的调试信息之前,这只是一个不好的DataBinding。

有什么建议么?

干杯,
贝里

System.Reflection.TargetParameterCountException was unhandled by user code
  Message=Parameter count mismatch.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.BaseObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\BaseObject.cs:line 51
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.ValueObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\ValueObject.cs:line 68
       at System.Collections.Hashtable.get_Item(Object key)
       at System.ComponentModel.TypeDescriptor.NodeFor(Object instance, Boolean createDelegator)
       at System.ComponentModel.TypeDescriptor.GetDescriptor(Object component, Boolean noCustomTypeDesc)
       at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
       at System.Windows.PropertyPath.ResolvePropertyName(String name, Object item, Type ownerType, Object context, Boolean throwOnError)
       at System.Windows.PropertyPath.ResolvePropertyName(Int32 level, Object item, Type ownerType, Object context)
       at MS.Internal.Data.PropertyPathWorker.GetInfo(Int32 k, Object item, SourceValueState& svs)
       at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
       at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
       at MS.Internal.Data.ClrBindingWorker.AttachDataItem()
       at System.Windows.Data.BindingExpression.Activate(Object item)
       at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
       at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
       at System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp)
       at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
       at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
       at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__3(Object sender, XamlObjectEventArgs args)
       at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException:


更新

下面是捕获异常的行:

var value = property.GetValue(this, null);


这是该方法的一部分

    public override int GetHashCode() {
        unchecked {
            IEnumerable<PropertyInfo> signatureProperties = GetSignatureProperties().ToArray();

            // It's possible for two objects to return the same hash code based on
            // identically valued properties, even if they're of two different types,
            // so we include the object's type in the hash calculation
            var hashCode = GetType().GetHashCode();

            foreach (var property in signatureProperties) {
                var value = property.GetValue(this, null);

                if (value != null)
                    hashCode = (hashCode * HASH_MULTIPLIER) ^ value.GetHashCode();
            }

            if (signatureProperties.Any())
                return hashCode;

            // If no properties were flagged as being part of the signature of the object,
            // then simply return the hashcode of the base object as the hashcode.
            return base.GetHashCode();
        }
    }

最佳答案

调用property.GetValue(时,第二个参数用于为索引器属性指定索引值。如果一个属性是一个索引器,并且您传递了null,那么您将获得一个异常。您需要确定哪些属性是索引器,并具有要作为第二个参数传递的值。

要添加引用,请根据documentation(在“例外”部分下),在以下情况下引发TargetParameterCountException


  索引中的参数数量与
  索引属性采用的参数。


这听起来像您的实际情况。

希望这可以帮助!

关于c# - 数据绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7654215/

10-11 13:05