就在我发布这个:
Automapper - Inheritance mapper not working with type converter
现在我正在尝试做@ jimmy-bogard在答案中所说的话,但不幸的是仍然没有成功。基本成员不会被映射。
吉米说:
但是,您可以使用ConstructUsing构建初始化 目标对象。或者是自定义的AfterMap,它也是继承的。只是 不转换。
这是我的新代码。
public void handleInput(MotionEvent motionEvent,LevelManager l, SoundManager sound, Viewport vp){
int pointerCount = motionEvent.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
int x = (int) motionEvent.getX(i);
int y = (int) motionEvent.getY(i);
if(l.isPlaying()) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
if (right.contains(x, y)) {
l.player.setPressingRight(true);
l.player.setPressingLeft(false);
} else if (left.contains(x, y)) {
l.player.setPressingLeft(true);
l.player.setPressingRight(false);
} else if (jump.contains(x, y)) {
l.player.startJump(sound);
} else if (shoot.contains(x, y)) {
if(l.player.pullTrigger()){
sound.playSound("shoot");
}
} else if (pause.contains(x, y)) {
l.switchPlayingStatus();
}
break;
case MotionEvent.ACTION_UP:
if (right.contains(x, y)) {
l.player.setPressingRight(false);
} else if (left.contains(x, y)) {
l.player.setPressingLeft(false);
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (right.contains(x, y)) {
l.player.setPressingRight(true);
l.player.setPressingLeft(false);
} else if (left.contains(x, y)) {
l.player.setPressingLeft(true);
l.player.setPressingRight(false);
} else if (jump.contains(x, y)) {
l.player.startJump(sound);
} else if (shoot.contains(x, y)) {
if(l.player.pullTrigger()){
sound.playSound("shoot");
}
} else if (pause.contains(x, y)) {
l.switchPlayingStatus();
}
break;
case MotionEvent.ACTION_POINTER_UP:
if (right.contains(x, y)) {
l.player.setPressingRight(false);
} else if (left.contains(x, y)) {
l.player.setPressingLeft(false);
} else if (shoot.contains(x, y)) {
//Handle shooting here
} else if (jump.contains(x, y)) {
//Handle more jumping stuff here later
}
break;
}
}else {// Not playing
//Move the viewport around to explore the map
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
if (right.contains(x, y)) {
vp.moveViewportRight(l.mapWidth);
} else if (left.contains(x, y)) {
vp.moveViewportLeft();
} else if (jump.contains(x, y)) {
vp.moveViewportUp();
} else if (shoot.contains(x, y)) {
vp.moveViewportDown(l.mapHeight);
} else if (pause.contains(x, y)) {
l.switchPlayingStatus();
}
break;
}
}
}
}
我错过了什么吗? 我使用ConstructUsing正确的方式吗?
任何帮助都将不胜感激。
答案 0 :(得分:2)
现在这样做不起作用,因为在ConstructUsing
中你创建了BaseClass
的实例,而当你将源映射到Derived1
类时,你需要一个实例Derived1
类的BaseClass
,public class BaseClassConstructor {
public static T Construct<T>(ResolutionContext context) where T : BaseClass, new() {
if (context == null || context.IsSourceValueNull)
return null;
var src = (SourceClass) context.SourceValue;
return new T() {
CommonAttr = src.SourceAttr
};
}
}
/* AutoMapperConfig.cs */
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
AutoMapper.Mapper.Initialize(config => {
config
.CreateMap<SourceClass, BaseClass>();
config
.CreateMap<SourceClass, DerivedClass1>()
.ForMember(dest => dest.Dummy, o => o.MapFrom(src => src.SourceAttr2))
.IncludeBase<SourceClass, BaseClass>()
.ConstructUsing((s, ctx) => BaseClassConstructor.Construct<DerivedClass1>(ctx));
config
.CreateMap<SourceClass, DerivedClass2>()
.ForMember(dest => dest.Dummy, o => o.MapFrom(src => src.SourceAttr2))
.IncludeBase<SourceClass, BaseClass>()
.ConstructUsing((s, ctx) => BaseClassConstructor.Construct<DerivedClass2>(ctx));
});
}
}
无法转换为(向下转发)。但是你可以这样做:
Construct
基本上我改变了你的ConstructUsing
方法以返回派生类的实例,然后使用Derived
,但不是在基类映射上,而是在nav {
margin: 0 auto;
text-align: center;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
color: white;
background: #787878;
background: linear-gradient(top, #787878 0%, #272727 100%);
background: -moz-linear-gradient(top, #787878 0%, #272727 100%);
background: -webkit-linear-gradient(top, #787878 0%, #272727 100%);
box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
padding: 0 20px;
border-radius: 25px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #A8A8A8;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block;
padding: 15px 50px;
color: white;
text-decoration: none;
}
nav ul ul {
background: #505050;
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul li {
float: left;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 8px 40px;
color: #fff;
}
nav ul ul li a:hover {
background: #A8A8A8;
}
nav ul ul ul {
position: absolute;
left: 100%;
top: 0;
}
nav ul ul.ul-right {
right: 0;
}
类映射上。