DataBinding error



我希望我的标题能更具描述性,但我很难理解下面的异常。

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

关于它,我唯一能补充的是,它发生在调用我的GetHashCode方法时,该方法触发一个方法,该方法通过反射对象的公共属性来派生哈希。在这种情况下,对象是从ViewModelBase类派生的,并且异常在它调用的Item属性上被命中,我猜它是IDataErrorInfo的索引器,但这只是猜测。

尽管如此,我的直觉是,在输出任何有用的调试信息之前,它只是一个糟糕的DataBinding。

有什么建议吗?

干杯,
Berryl

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:UsersLord & MasterDocumentsProjectsSmacktrunksrcCoreDomainSuperTypesEntityImplBaseObject.cs:line 51
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.ValueObject.GetHashCode() in C:UsersLord & MasterDocumentsProjectsSmacktrunksrcCoreDomainSuperTypesEntityImplValueObject.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,那么您将得到一个异常。您需要确定哪些属性是索引器,并有一个值作为第二个参数传入。

为了添加引用,根据文档(在Exceptions部分下(,当:时抛出TargetParameterCountException

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

这听起来像是你的真实情况。

希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新