Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用类型参数保存操作的集合-无法将IEnumerable转换为IEnumerable_C# - Fatal编程技术网

C# 使用类型参数保存操作的集合-无法将IEnumerable转换为IEnumerable

C# 使用类型参数保存操作的集合-无法将IEnumerable转换为IEnumerable,c#,C#,在Asp.Net http中间件的以下类中,有一个类型参数为ExportResult的方法。。。并导出一个字典来存储具有不同类型参数的方法 public class ExportMiddleware : IMiddleware { public Dictionary<string, Func<IEnumerable<dynamic>, string, DataSourceLoadOptionsBase, HttpContext, Task>> Expo

在Asp.Net http中间件的以下类中,有一个类型参数为ExportResult的方法。。。并导出一个字典来存储具有不同类型参数的方法

public class ExportMiddleware : IMiddleware
{
    public Dictionary<string, Func<IEnumerable<dynamic>, string, DataSourceLoadOptionsBase, HttpContext, Task>> Exports => 
        new Dictionary<string, Func<IEnumerable<dynamic>, string, DataSourceLoadOptionsBase, HttpContext, Task>>();

    public ExportMiddleware()
    {
        Exports.Add("download1", (IEnumerable<dynamic> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context) =>
                                  ExportResult<MyEntity>(source, format, dataOptions, context)); // Error
                                  // Cannot convert IEnumerable<dynamic> to IEnumerable<MyEntity>
    }

    private async Task ExportResult<T>(IEnumerable<T> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
    {
        // ....
        // report.DataSource = loadedData.data.Cast<T>();
        // ....
    }

    // Consume the dictionary
    public Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        // ....
        var path = context.Request.Path.ToString();
        if (Exports.TryGetValue(path, out var func))
            return func(source, format, options, context);
        return next(context);
    }
如何解决源上无法将IEnumerable转换为IEnumerable的错误? 有没有一种方法不使用dynamic?没有类类型参数 如果您的代码知道源代码将是IEnumerable,则将其转换为:

    Exports.Add("download1", (IEnumerable<dynamic> source, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context) =>
                              ExportResult((IEnumerable<MyEntity>)source, format, dataOptions, context));

在上述任何一种情况下,您可能需要重新思考dynamic是否真的是您想要的。由于IEnumerable是协变的,因此IEnumerable在您共享的所有代码中都可以正常工作。

是的,我可以在代码中将dynamic更改为object。在这种情况下,它们是否完全相同?是的,只要不使用动态类型的任何功能,它们的行为应该相同。有一组相对较小的情况下,我会使用动态。一种是基于仅在运行时已知的对象类型,启用更好的泛型类型推断。另一种是与来自其他语言(如python或javascript)的对象进行互操作时,您对该对象有所了解,但无法为其指定实际的编译时类型。在几乎任何其他情况下,我发现动态都是不必要的,而且往往是危险的?
private async Task ExportResult<T>(IEnumerable<dynamic> dynamicSource, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
{
    var source = (IEnumerable<T>)dynamicSource;
private async Task ExportResult<T>(IEnumerable<dynamic> dynamicSource, string format, DataSourceLoadOptionsBase dataOptions, HttpContext context)
{
    var source = dynamicSource.Cast<T>();