小编Cha*_*Shi的帖子

类型'***'不能分配给Autofac中的服务'***'

我现在使用System.Linq.Dynamic在我的项目中进行动态查询.我使用Autofac作为我的默认IOC容器.但是现在我在注册通用组件时遇到了问题,这是我的代码:

界面:

public interface IDynamicQuery
{
    IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class;
}
Run Code Online (Sandbox Code Playgroud)

班级:

public class DynamicQuery :IDynamicQuery
{
    public DynamicQuery(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }
    private  readonly IUnitOfWork unitOfWork;

    public IQueryable<T> CreateDynamicQuery<T>(string propertyName, string propertyValue, Expression<Func<T, bool>> where) where T:class
    {
        var appRepository = unitOfWork.Repository<T>();

        IQueryable<T> queryResult = null;
        if (propertyName.Contains('$'))  
            propertyName = propertyName.Replace('$', '.');
        queryResult = appRepository.GetMany(where).Where("" + propertyName + ".Contains(\"" + propertyValue + "\")");
        return queryResult;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在应用程序启动条目中注册它们:

builder.RegisterType<IDynamicQuery>().As<DynamicQuery>().InstancePerHttpRequest();
Run Code Online (Sandbox Code Playgroud)

但是当我基于MVC 4启动我的项目时,它会抛出一个例外: …

c# autofac

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

在.NET中有多少个异步开发模型?

我正在学习使用C#的异步编程,我经常使用BeginInvoke,但我不太确定创建异步应用程序的其他方法.

我已经就此问了一个问题,请参阅以下链接了解更多详情:

如何从BeginInvoke返回T值?

在上面的链接中,Gravell说有四种异步开发模型

至少有4个,然后 - 常规回调(非APM,非EAP)也不常见

但溢流说有三个:

.NET中有3种异步开发模型

  • APM - (BeginXXX/ EndXXX)您在此处使用,当长时间运行的任务完成时,它会在EndXXX方法中回调您的代码

  • EAP - 基于事件.在此模型中,当长时间运行的任务完成时,将引发一个事件以通知您的代码.

  • TPL - .NET 4中的新功能,这是基于任务的版本.它看起来最像是使用流畅的界面对客户端代码进行同步编程.它使用回调你的代码ContinueWith.

有人可以帮我吗?

我搜索过很多google.com,但实际上他们使用的BeginInvoke最多.谢谢你的帮助.

c# asynchronous model

8
推荐指数
1
解决办法
743
查看次数

参数"***"未绑定在指定的LINQ to Entities查询表达式中

我正在我的项目中进行常见查询.我使用Expression来构建我的查询树,代码列表如下:

 public IList<Book> GetBooksFields(string fieldName, string fieldValue)
    {
        ParameterExpression paramLeft = Expression.Parameter(typeof(string), "m." + fieldName);
        ParameterExpression paramRight = Expression.Parameter(typeof(string), "\"" + fieldValue + "\"");

        ParameterExpression binaryLeft = Expression.Parameter(typeof(Book),"m");
        BinaryExpression binaryExpr = Expression.Equal(paramLeft, paramRight);

        var expr = Expression.Lambda<Func<Book, bool>>(binaryExpr, binaryLeft);

        return bookRepository.GetMany(expr).ToList();

    }
Run Code Online (Sandbox Code Playgroud)

但是当我调用我的GetBooksFields方法时,它会抛出一个异常,如下所示: 在此输入图像描述

我调试了expr变量并获得了正确的表达式:{ m => (m.Name == "sdf")},这是我想要的,但我不知道为什么我得到错误,thx.

.net c# linq expression expression-trees

6
推荐指数
1
解决办法
4871
查看次数

如何裁剪图像并保存到WPF中的ImageSource?

我是WPF的新学员.我有一个问题.我有一个图像,宽度:360,高度:360.在这里,我想裁剪如下图像:

(0,0)到(120,120)保存到第一个ImageSource对象,

(120,0)到(240,120)保存到第二个ImageSource对象,

(240,0)到(360,120)保存到第三个ImageSource对象;,

......

请参阅下图中的更多详细信息: 在此输入图像描述

我的代码示例如下:

    private void CutImage(string img)
    {
        int iLeft = 0;
        int iTop = 0;
        int count = 0;

        Image thisImg = new Image();
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(img, UriKind.Relative);
        src.CacheOption = BitmapCacheOption.OnLoad;
        src.EndInit();
        thisImg.Source = src;

        for (int i = 0; i < 3; i++)
        {
            iTop = i * 120;
            for (int j = 0; j < 3; j++)
            {
                iLeft = j * 120;

                Canvas canvas = …
Run Code Online (Sandbox Code Playgroud)

wpf image crop

4
推荐指数
1
解决办法
2万
查看次数

如何在C#中将DataSet转换为实体?

在这里,我提出了第一种方法:即,在DataSet上使用foreach子句来获取项目并填写您的实体.但是当事物发生变化时,这种方法无法重复使用.

所以我认为也许反思应该是我的方案的最佳方法.以下是详细信息:

1.我的实体定义如下:

using System.ComponentModel;

namespace WarningServiceForYM.Model
{
   public class StuffEntity
   {
    [Description("??ID")]
    public string EnterpriseID { get; set; }  

     [Description("????")]
    public string FridgeID { get; set; }        

    [Description("????")]
    public string FridgeName { get; set; }   

    [Description("????")]
    public string PhoneNumber { get; set; }  

    [Description("????")]
    public string EquipmentName { get; set; } 

    [Description("????")]
    public string PickingParams { get; set; }   

    [Description("????")]
    public string TempOne { get; set; }         

    [Description("????")]
    public string TempTwo { get; set; }         

    [Description("????")]
    public string TempThree …
Run Code Online (Sandbox Code Playgroud)

c# entity dataset

4
推荐指数
1
解决办法
5706
查看次数

由于类型比较错误,redis lua无法正常工作

我想通过在Python中使用redis lua脚本来减少我的存储,我的代码如下:

def lua_storage():
conn = redis_conn()
lua = """
        local storage = redis.call('get','storage')
        if  (storage ~= nil) then
            if tonumber(storage) >= 0 then
                return redis.call('decr','storage')
            else
                return 'storage is zero now, can reply decr action'
            end
        else
            redis.call('set','storage',10)
        end
        """
result = conn.eval(lua,0)
print(result)
Run Code Online (Sandbox Code Playgroud)

上面的代码很简单,是一个控制商品存储的lua脚本,如果商品没有存储,脚本会将其存储设置为10,当用户访问此脚本时,存储将减1直至为0。当存储为 0 时,用户将收到“存储现在为零,可以回复 decr 操作”消息。

当我在 python 中运行这个方法时,出现以下异常:

Traceback (most recent call last):
File "D:/mytools/luaredis/mytest.py", line 53, in <module>
lua_storage()
File "D:/mytools/luaredis/mytest.py", line 47, in lua_storage
result = conn.eval(lua,0)
File "C:\Python27\lib\site-packages\redis\client.py", line 2067, in eval …
Run Code Online (Sandbox Code Playgroud)

lua redis

3
推荐指数
1
解决办法
2308
查看次数

如何从BeginInvoke返回T值?

我想写一个类来简化异步编程,比如string s = mylib.BeginInvoek(test,"1"); 这是我的代码:

   public T BeginInvokeExWithReturnValue<T>(Func<T> actionFunction)
    {
        ExecWithReturnType<T> execWtihReturnValue = new ExecWithReturnType<T>(actionFunction);
        IAsyncResult iar = execWtihReturnValue.BeginInvoke(new AsyncCallback(EndInvokeExWithReturnValue<T>), execWtihReturnValue);
        // how to code here to return value
    }

    private void EndInvokeExWithReturnValue<T>(IAsyncResult iar)
    {
        ExecWithReturnType<T> execWtihReturnValue = (ExecWithReturnType<T>)iar.AsyncState;
        execWtihReturnValue.EndInvoke(iar);
    }
Run Code Online (Sandbox Code Playgroud)

这个BeginInvokeExWithReturnValue函数没有输入参数,但返回一个值,但我不知道如何从BeginInvokeExWithReturnValue函数返回一个值.知道这一点的人,你能帮助我吗?非常感谢.

c# return begininvoke

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

C#Closure有效吗?

可能重复:
C#在foreach中重用变量是否有原因?
循环操作列表

今天我遇到了关于C#foreach函数的问题,它并没有像我预期的那样给我正确的结果.这是代码:

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        int[] data = new int[] { 1, 2, 3, 4, 5 };
        List<Func<int>> actions = new List<Func<int>>();
        foreach (int x in data)
        {
            actions.Add(delegate() { return x; });
        }
        foreach (var foo in actions)
        {
            Console.WriteLine(foo());   
        }
        Console.ReadKey();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

当我在控制台应用程序中运行它并且它在屏幕上打印了5个5.为什么?我只是无法理解.曾经问过一些人,他们只是说这个代码中有封闭,但我对此并不十分清楚,我记得在javascript中,我经常遇到封闭,但在上面的代码中,为什么会有封闭?谢谢.

c# closures

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