对象是一个字段,但使用起来像一个类型
本文关键字:一个 起来 类型 字段 对象 | 更新日期: 2023-09-27 18:24:19
我试图将数据绑定到表单,但一直收到以下错误:
AbDesktop.FrmCreditCards.tmpData是一个"字段",但使用起来像"类型"。
这就是导致错误的代码,任何帮助都将不胜感激。
public partial class FrmCreditCards : Form
{
tmpCreditCardData tmpData = new tmpCreditCardData() { CreditCardName = "Reece" };
private HotelConferenceEntities db = new HotelConferenceEntities();
private tblCreditCard databaseAccessObject;
private tmpData dataTransferObject;
public void databinding()
{
// Create forward map using AutoMapper - from database to dataTransfer
AutoMapper.Mapper.CreateMap<tblCreditCard, tmpData>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CreditCardName));
// Create reverse map using AutoMapper - from dataTransfer to database
AutoMapper.Mapper.CreateMap<tmpData, tblCreditCard>()
.ForMember(dest => dest.CreditCardName, opt => opt.MapFrom(src => src.Name));
}
CreateMap的通用参数是问题所在。您必须使用类型tmpCreditCardData
,而不是编译错误所说的变量名tmpData
。
// Create forward map using AutoMapper - from database to dataTransfer
AutoMapper.Mapper.CreateMap<tblCreditCard, tmpCreditCardData>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CreditCardName));
// Create reverse map using AutoMapper - from dataTransfer to database
AutoMapper.Mapper.CreateMap<tmpCreditCardData, tblCreditCard>()
.ForMember(dest => dest.CreditCardName, opt => opt.MapFrom(src => src.Name));
类型和字段使用相同的名称-将字段tmpData重命名为其他名称-即:
tmpCreditCardData tmpData2 = new tmpCreditCardData
{ CreditCardName = "Reece" };
有一些关于约定的评论-约定指定类应该以大写字母开头,并像一样命名
MyFavouriteClass
并且字段应该像一样命名
myFavouriteField
(以小写字母开头)
有些人还用下划线来区分私有字段
private MyFavouriteClass _myFavouriteField
如果你坚持他们,你就不应该遇到这个问题。