小编Dev*_*ion的帖子

在4.0项目中引用.net框架4.5.1程序集

我如何使4.0项目具有4.5参考.在单元测试中,我无法构建解决方案,它给了我这个警告.

警告2无法解析主要参考"PR.Wallet",因为它是针对".NETFramework,Version = v4.5.1"框架构建的.这是比当前目标框架".NETFramework,Version = v4.0"更高的版本.PR.Wallet.Tests

.net c#

16
推荐指数
3
解决办法
2万
查看次数

存储库,删除对象,模型与ID

在存储库中,最佳和最合乎逻辑的方法是什么?

使用实体删除还是按ID删除?

public void Delete(Entity item);

VS.

public void Delete(int Id);
Run Code Online (Sandbox Code Playgroud)

我只是想知道什么是最佳实践,应该通过Entity删除(在删除之前先找到对象即可实现)还是通过Id删除(在方法上搜索对象然后删除)。

c# wcf entity-framework repository-pattern

5
推荐指数
1
解决办法
929
查看次数

编译表达式比反射慢

我有一个具有动态集的PropertyInfo.SetValue.意味着要设置的值是未知的.

我从互联网上得到了这样的方法.

private static Action<object, object> CreateSetAccess(MethodInfo method)
{
    var obj = Expression.Parameter(typeof(object), "o");
    var value = Expression.Parameter(typeof(object));

    Expression<Action<object, object>> expr =
        Expression.Lambda<Action<object, object>>(
            Expression.Call(
                Expression.Convert(obj, method.DeclaringType),
                method,
                Expression.Convert(value, method.GetParameters()[0].ParameterType)),
            obj,
            value);

    return expr.Compile();
}
Run Code Online (Sandbox Code Playgroud)

这样做是创建一个表达式并对其进行编译,但是使用参数类型转换对象.

我像这样消费它.

var method2 = CreateSetAccess(property.GetSetMethod());
method2(response, valueToSet);
Run Code Online (Sandbox Code Playgroud)

发生的事情似乎是慢了 PropertyInfo.SetValue

这是我的基准

var xpathNavigator = XmlHelper.CreateXPathDocument(serviceResponse).CreateNavigator();
foreach (var propertyInformation in propertyInformationSource)
{
    // Gets the node using the NodePath provided in the Attribute
    var attr = propertyInformation.Value;
    var pathValue = xpathNavigator.SelectSingleNode(attr.NodePath);
    if (pathValue == null)
        continue; …
Run Code Online (Sandbox Code Playgroud)

c# performance expression-trees

-1
推荐指数
1
解决办法
644
查看次数