- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过以下代码使用 Expression
创建一个简单的映射器:
public static class MyUtility {
public static Action<TSource, TTarget> BuildMapAction<TSource, TTarget>(IEnumerable<PropertyMap> properties) {
var sourceInstance = Expression.Parameter(typeof(TSource), "source");
var targetInstance = Expression.Parameter(typeof(TTarget), "target");
var statements = BuildPropertyGettersSetters(sourceInstance, targetInstance, properties);
Expression blockExp = Expression.Block(new[] { sourceInstance, targetInstance }, statements);
if (blockExp.CanReduce)
blockExp = blockExp.ReduceAndCheck();
blockExp = blockExp.ReduceExtensions();
var lambda = Expression.Lambda<Action<TSource, TTarget>>(blockExp, sourceInstance, targetInstance);
return lambda.Compile();
}
private static IEnumerable<Expression> BuildPropertyGettersSetters(
ParameterExpression sourceInstance,
ParameterExpression targetInstance,
IEnumerable<PropertyMap> properties) {
var statements = new List<Expression>();
foreach (var property in properties) {
// value-getter
var sourceGetterCall = Expression.Call(sourceInstance, property.SourceProperty.GetGetMethod());
var sourcePropExp = Expression.TypeAs(sourceGetterCall, typeof(object));
// value-setter
var targetSetterCall =
Expression.Call(
targetInstance,
property.TargetProperty.GetSetMethod(),
Expression.Convert(sourceGetterCall, property.TargetProperty.PropertyType)
);
var refNotNullExp = Expression.ReferenceNotEqual(sourceInstance, Expression.Constant(null));
var propNotNullExp = Expression.ReferenceNotEqual(sourcePropExp, Expression.Constant(null));
var notNullExp = Expression.And(refNotNullExp, propNotNullExp);
var ifExp = Expression.IfThen(notNullExp, targetSetterCall);
statements.Add(ifExp);
}
return statements;
}
}
我觉得一切正常,但是当我尝试对其进行测试时,我只是遇到空引用异常。测试对象及方法:
public class UserEntity {
public string Name { get; set; }
public string Family { get; set; }
public int Age { get; set; }
public string Nickname { get; set; }
}
public class UserModel {
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Nickname { get; set; }
}
public static class CallTest {
public static void Call() {
var entity = new UserEntity {
Name="Javad",
Family="Amiry",
Age = 25,
Nickname = "my nickname is here",
};
var model = new UserModel();
var map1 = new PropertyMap {
SourceProperty = entity.GetType().GetProperty("Age"),
TargetProperty = model.GetType().GetProperty("Age"),
};
var map2 = new PropertyMap {
SourceProperty = entity.GetType().GetProperty("Nickname"),
TargetProperty = model.GetType().GetProperty("Nickname"),
};
var action = MyUtility.BuildMapAction<UserEntity, UserModel>(new[] {map1, map2});
action(entity, model); // here I get the error System.NullReferenceException: 'Object reference not set to an instance of an object.'
}
}
你知道那里发生了什么吗?我错过了什么?
注意:我不能使用第三方映射器(如 AutoMapper)
最佳答案
问题是由这一行引起的:
Expression blockExp = Expression.Block(new[] { sourceInstance, targetInstance }, statements);
使用的 Expression.Block
的第一个参数overload 表示 block 的局部变量。通过在那里传递 lambda 参数,您只需定义 2 个本地未分配变量,因此在执行时定义 NRE。您可以通过检查 VS locals/watch 窗口中的 lambda 表达式 DebugView
来看到这一点,在您的示例调用中它看起来像这样:
.Lambda #Lambda1<System.Action`2[ConsoleApp3.UserEntity,ConsoleApp3.UserModel]>(
ConsoleApp3.UserEntity $source,
ConsoleApp3.UserModel $target) {
.Block(
ConsoleApp3.UserEntity $source,
ConsoleApp3.UserModel $target) {
.If (
$source != null & .Call $source.get_Age() .As System.Object != null
) {
.Call $target.set_Age((System.Int32).Call $source.get_Age())
} .Else {
.Default(System.Void)
};
.If (
$source != null & .Call $source.get_Nickname() .As System.Object != null
) {
.Call $target.set_Nickname((System.String).Call $source.get_Nickname())
} .Else {
.Default(System.Void)
}
}
}
注意 block 内source
和target
的重新定义。
使用正确的重载后:
Expression blockExp = Expression.Block(statements);
现在的 View 是这样的:
.Lambda #Lambda1<System.Action`2[ConsoleApp3.UserEntity,ConsoleApp3.UserModel]>(
ConsoleApp3.UserEntity $source,
ConsoleApp3.UserModel $target) {
.Block() {
.If (
$source != null & .Call $source.get_Age() .As System.Object != null
) {
.Call $target.set_Age((System.Int32).Call $source.get_Age())
} .Else {
.Default(System.Void)
};
.If (
$source != null & .Call $source.get_Nickname() .As System.Object != null
) {
.Call $target.set_Nickname((System.String).Call $source.get_Nickname())
} .Else {
.Default(System.Void)
}
}
}
NRE 消失了。
那是关于原始问题的。但是生成的代码看起来很丑陋而且不是最理想的。源对象空检查可以围绕整个 block 进行,类型转换和值空检查可以只在需要时进行。作为奖励,我会这样写:
public static Action<TSource, TTarget> BuildMapAction<TSource, TTarget>(IEnumerable<PropertyMap> properties)
{
var source = Expression.Parameter(typeof(TSource), "source");
var target = Expression.Parameter(typeof(TTarget), "target");
var statements = new List<Expression>();
foreach (var propertyInfo in properties)
{
var sourceProperty = Expression.Property(source, propertyInfo.SourceProperty);
var targetProperty = Expression.Property(target, propertyInfo.TargetProperty);
Expression value = sourceProperty;
if (value.Type != targetProperty.Type)
value = Expression.Convert(value, targetProperty.Type);
Expression statement = Expression.Assign(targetProperty, value);
// for class/interface or nullable type
if (!sourceProperty.Type.IsValueType || Nullable.GetUnderlyingType(sourceProperty.Type) != null)
{
var valueNotNull = Expression.NotEqual(sourceProperty, Expression.Constant(null, sourceProperty.Type));
statement = Expression.IfThen(valueNotNull, statement);
}
statements.Add(statement);
}
var body = statements.Count == 1 ? statements[0] : Expression.Block(statements);
// for class.interface type
if (!source.Type.IsValueType)
{
var sourceNotNull = Expression.NotEqual(source, Expression.Constant(null, source.Type));
body = Expression.IfThen(sourceNotNull, body);
}
// not sure about the need of this
if (body.CanReduce)
body = body.ReduceAndCheck();
body = body.ReduceExtensions();
var lambda = Expression.Lambda<Action<TSource, TTarget>>(body, source, target);
return lambda.Compile();
}
生成更像 C# 的代码:
.Lambda #Lambda1<System.Action`2[ConsoleApp3.UserEntity,ConsoleApp3.UserModel]>(
ConsoleApp3.UserEntity $source,
ConsoleApp3.UserModel $target) {
.If ($source != null) {
.Block() {
$target.Age = $source.Age;
.If ($source.Nickname != null) {
$target.Nickname = $source.Nickname
} .Else {
.Default(System.Void)
}
}
} .Else {
.Default(System.Void)
}
}
关于c# - 在相同属性上将一个对象映射到另一个对象的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46872076/
我知道这在 Linux 上是可能的。我尝试使用 open("E:", 0); 和 open("E:\\", 0); 但它返回为 -1。我想将 DVD 作为一个大文件来读取,而不是将其用作文件系统。 最
我正在尝试编译一个包含 CUDA 代码的小型库。 我已成功将其编译为共享库,但我真正需要的是静态库。 我有两个源文件: main.c:包含一个用C编写的测试函数。我用gcc编译这个文件 mai
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我正在使用 MingW 在 Windows 上编写 C 程序,并希望使用 EXPAT XML 库。我想静态编译我的程序,所以我需要静态 .a 库。 有什么方法可以将 EXPAT 编译成 Windows
我想将结果限制为 KEY_HOMEID 等于 journalId 的结果。我已经研究了几天,如有任何帮助,我们将不胜感激。 public Cursor fetchAllNotes(String jou
我一直在寻找这个信息,但是由于可以通过 homebrew 和 pip 安装额外的包和 python 版本,我感觉我的环境很乱向上。此外,很久以前,我用 sudo pip install 和 sudo
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在尝试合并目录中的所有 *.pdf : gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=Total_Files.pdf -dBATCH *
所以我有一个简单的图像缩略图着陆页,例如: 在我的网站上,4 个并排显示在桌面上。如何强制它们在移动设备 View 中成对出现在一行中? 所以:桌面: #### 手机: ## ## 最佳答案
我正在使用 Ubuntu 21.04。我已删除 /usr/bin/python3和 /usr/lib/python3/因为某些软件包在二进制文件中出现错误。我的意思是重新安装python3到一个新的状
是否可以在 Windows 上使用 gyp 将 googles v8 构建为共享库(msvc 2012)?我试过的一切都不起作用。我试过的: python build\gyp_v8 -Dcompone
我需要将 rubygems 从 1.3.5 更新到 1.4.2 但显然 rubygems update 只是将您更新到最新版本 如何更新到 1.4.2? 最佳答案 您可以使用 RVM 安装特定
我还没有找到太多关于它的信息,但我看到了一些提示,表明可以在 iPhone 应用程序中使用 NSTask。如果可能的话,我将如何去做? 我不想越狱我的 iPhone,但我正在开发的应用程序仅供内部使用
我在 UIWebView 中有一个 map 图像。它默认加载在左上角。我希望它在 UIWebView 的中心初始化。 有人知道怎么做吗? 谢谢! 最佳答案 如果 map 图像是页面中唯一的内容,它是否
如何公开 NodePort 类型的服务上网没有 使用类型 LoadBalancer ?我发现的每个资源都是通过使用负载均衡器来完成的。但我不希望负载平衡对我的用例来说既昂贵又不必要,因为我正在运行 p
是否可以将 View 变成可编辑的,例如 this image ? 我知道我可以使用 GridView 来做到这一点。但是,我正在尝试使用 TableRows 来做到这一点,这可能吗? 编辑:我真正想
假设我已将 Heroku 应用程序扩展为 1 个工作进程,但如何指定具有特定名称的 rake 任务应作为工作进程运行? 最佳答案 在你的项目中创建一个 Procfile,然后像这样将 rake 任务放
目前,我在 GitHub 上一个项目的 README.md 文件中使用此 Markdown 文本: See the docs of [testthat][3] on how to write unit
我正在尝试使用一些到 uint8_t 的转换将 IPv4 转换为 IPv6。我知道 IPv4 有 4 个字节,IPv6 有 2 个字节的 16 个无符号整数,但我找不到它们转换的方法。 #includ
我是编程新手,目前正在学习 C。您能帮我解决以下案例吗? 一个例子是,如果用户输入“cbamike”,我想将其分成两个字符串:cba 和 mike。 我尝试了下面的代码,但它不起作用: #includ
我是一名优秀的程序员,十分优秀!