【发布时间】: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。
【问题讨论】:
-
哪一行抛出异常?