Une异常de type'System.Reflection.TargetException'

时间:2017-08-26 11:34:11

标签: c# mvvm

我对下面的例外情况有疑问:

  

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,ouuneprivriétéestunepropriétéd'examplemais obj a la valeur null。

1 个答案:

答案 0 :(得分:0)

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

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列表中。例如。它尝试使用从源对象中的idTypeItem属性检索的值,方法是将目标对象的每个属性设置为该值。

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

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参数值来自哪里,但可能您确实有一个视图对象,您可以传递给该方法。否则,你认为这些价值会来自哪里?