小编and*_*tia的帖子

c#从字符串表示Lambda到Func <T>

我尝试从string实现e动态Funx表达式

Expression<Func<CustomerDto, object>> expr = (src) => src.Customer.Id;
Func<CustomerDto, object> delg = expr.Compile();
var id = delg.Invoke(customerListDtos[0]);
Run Code Online (Sandbox Code Playgroud)

并返回id(es.123)

所以现在我尝试从string创建表达式

    public Expression<Func<T, object>> GetLambda<T>(string property)
    {

        var param = Expression.Parameter(typeof(T), "p");
        Expression body = param;
        foreach (var member in property.Split('.'))
        {
            body = Expression.PropertyOrField(body, member);
        }
        //return Expression.Lambda(body, param);

        //Expression parent = Expression.Property(param, property);
        Expression parent = Expression.Lambda(body, param);
        //if (!parent.Type.IsValueType)
        //{
        //    return Expression.Lambda<Func<T, object>>(parent, param);
        //}
        var convert = Expression.Convert(parent, typeof(object));
        return Expression.Lambda<Func<T, object>>(convert, param);
    }
Run Code Online (Sandbox Code Playgroud)

因此,如果我从GetLambda调用结果,则输出不是123,而是

 var data …
Run Code Online (Sandbox Code Playgroud)

c# linq expression

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

取消从 Angular 到 .NET Core 的长操作

我尝试从 Angular 客户端(使用 Observable -> 取消订阅...)发送取消请求(在基于 Abp Boilerplate 的 c# AspNetCore 上)来取消长任务操作,但 API 不触发。

    [HttpPost]
        public async Task<GetDataOutputDto> GetDataLongOperation(CustomInputDto input)
        {
            try
            {
                var tokenSource = new CancellationTokenSource(); // _httpContextAccessor.HttpContext.RequestAborted;
                tokenSource.CancelAfter(2500);

                var token = _httpContextAccessor.HttpContext.RequestAborted; //tokenSource.Token;

                var settings = new JsonSerializerSettings
                {
                    Error = (sender, args) => {
                        args.ErrorContext.Handled = true;
                    },
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };


                //... remove for brev
        }
Run Code Online (Sandbox Code Playgroud)

在有角的一侧

                this.subscription = this.loadData(undefined).subscribe(res=>{
                    console.log('data loaded!');
                    this.localData = res.data;
                    //this._rawData = this.localData;
                    this.loadItems();
                    
                });

                setTimeout(() => {
                    console.log('TEST stop …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core angular abp

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

标签 统计

c# ×2

abp ×1

angular ×1

asp.net-core ×1

expression ×1

linq ×1