【问题标题】:Une exception de type 'System.Reflection.TargetException'一个异常de类型'System.Reflection.TargetException'
【发布时间】:2018-02-04 07:13:28
【问题描述】:

我对下面给出的异常有疑问:

System.Reflection.TargetException

所以,首先,我希望为数据库中的 add 方法开发一个通用方法,但是我在这个 ligne 中遇到了问题,所以我的 genericADD :

namespace MyProjectWPFMVVM.ViewsModels{
class PropertiesVMTypeItem : ViewModelBase
{
  public   int idTypeItem {
      get { return idTypeItem; }
      set
      {
          idTypeItem = 10;
          OnPropertyChanged("idTypeItem");
      }
  }
    public string DesignationTypeItem
    {
        get { return DesignationTypeItem; } 
        set
        {
            DesignationTypeItem = "sss";
            OnPropertyChanged("DesignationTypeItem"); }
    }
    public int MaxNumberConnectionsTypeItem
    {
        get { return MaxNumberConnectionsTypeItem; }
        set
        {
            MaxNumberConnectionsTypeItem=1;
            OnPropertyChanged("MaxNumberConnectionsTypeItem"); }
    }
}}
//and for my class model :
namespace MyProjectWPFMVVM.Models{  
public partial class im_type_items
{
    public im_type_items()
    {
        this.im_characteristics_items = new HashSet<im_characteristics_items>();
        this.im_items = new HashSet<im_items>();
    }

    public int idTypeItem { get; set; }
    public string DesignationTypeItem { get; set; }
    public byte[] SymbolTypeItem { get; set; }
    public  int MaxNumberConnectionsTypeItem { get; set; }

    public virtual ICollection<im_characteristics_items> im_characteristics_items { get; set; }
    public virtual ICollection<im_items> im_items { get; set; }
}}//and this is my appel methode in VM :
public void GenericAjoutt(){
IList<PropertyInfo> propertiesForModel = 
GenericAjout.GetPropertiesForModel<im_type_items>();
        IList<PropertyInfo> propertiesForView = GenericAjout.GetPropertiesForView<PropertiesVMTypeItem>();
        var newTypeItem = GenericAjout.CreateNewRowInModel<im_type_items>(propertiesForView, propertiesForModel);
        ImItemsModel.SaveChanges();


    }//and my problem is : namespace MyProjectWPFMVVM.Method 
{public static class GenericAjout
{


    private static Dictionary<Type, IList<PropertyInfo>> ModelDictionary = new Dictionary<Type, IList<PropertyInfo>>();
    public static IList<PropertyInfo> GetPropertiesForModel<T>()
    {
        var type = typeof(T);
        if (!ModelDictionary.ContainsKey(typeof(T)))
        {
            ModelDictionary.Add(type, type.GetProperties().ToList());
        }
        return ModelDictionary[type];
    }
    private static Dictionary<Type, IList<PropertyInfo>> ViewPropertiesDictionary = new Dictionary<Type, IList<PropertyInfo>>();
    public static IList<PropertyInfo> GetPropertiesForView<T>()
    {
        var type = typeof(T);
        if (!ViewPropertiesDictionary.ContainsKey(typeof(T)))
        {
            ViewPropertiesDictionary.Add(type, type.GetProperties().ToList());
        }
        return ViewPropertiesDictionary[type];
    }
    public static T CreateNewRowInModel<T>(IList<PropertyInfo> propertiesView, IList<PropertyInfo> propertiesModel  ) where T  : new() 
    {
        T item = new T();
            foreach (var p in propertiesView)
            {
                foreach (var property in propertiesModel)
                {
                    property.SetValue(item, p.GetValue(property.Name)); // erreur L’objet ne correspond pas au type cible, ou une propriété est une propriété d’instance mais obj a la valeur null.
                }
            }
            return item;
    }

}

}

所以请帮忙。 我编辑问题 所以是 property.SetValue(item, p.GetValue(property.Name)); // erreur L'objet ne 对应 pas au type cible, ou une propriété est une propriété d'instance mais obj a la valeur null。

【问题讨论】:

  • 哪一行抛出异常?

标签: c# mvvm


【解决方案1】:

您的问题有点难以理解,但如果我理解正确,可以归结为:

foreach (var p in propertiesView)
{
    foreach (var property in propertiesModel)
    {
        property.SetValue(item, p.GetValue(property.Name)); // erreur L’objet ne correspond pas au type cible, ou une propriété est une propriété d’instance mais obj a la valeur null.
    }
}

也就是说,当您调用 property.GetValue() 时,您会收到一个异常报告,报告您传入的对象没有您尝试设置的属性的正确类型。

考虑到代码,这是有道理的,它似乎正在传递一个string 值,就好像那是该属性所在的对象。即使已解决,您还有另一个问题,即对于 propertiesView 列表中的每个属性,代码尝试使用该属性的值并在 propertiesModel 列表中设置 each 属性.例如。它尝试使用从源对象中的idTypeItem 属性检索到的值,方法是将目标对象的 every 属性设置为该值。

在我看来,相反,您应该传入要从中复制属性的对象,并且只想在名称相同的情况下设置属性。例如:

public static T CreateNewRowInModel<T>(
    IList<PropertyInfo> propertiesView, IList<PropertyInfo> propertiesModel, object source)
    where T : new() 
{
    T item = new T();

    foreach (var p in propertiesView)
    {
        object value = p.GetValue(source);

        foreach (var property in propertiesModel)
        {
            // Optional, not shown:
            // For added security, check that the types are the same also

            if (property.Name == p.Name)
            {
                property.SetValue(item, value);
                break;
            }
        }
    }

    return item;
}

我在您的代码中看不到source 参数值的来源,但大概您心中确实有一个可以传递给该方法的视图对象。否则,您认为这些值会从哪里来?

【讨论】:

猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 2018-12-22
  • 2012-02-06
  • 1970-01-01
  • 2019-09-08
相关资源
最近更新 更多