如何指定匿名对象作为通用参数? [英] How to specify anonymous object as generic parameter?

查看:80
本文介绍了如何指定匿名对象作为通用参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下任务:

var task = _entityManager.UseRepositoryAsync(async (repo) => 
{
    IEnumerable<Entity> found = //... Get from repository

    return new 
    {
        Data = found.ToList()
    };
}

task是什么类型?

实际上,结果是:System.Threading.Tasks.Task<'a>

其中'a是匿名类型:{ List<object> Data }

如何在不使用var的情况下明确声明此类型?

我尝试过Task<a'> task = ...Task<object> task = ...,但无法对其进行编译.

我为什么需要这样做?

我有一个方法(UseApplicationCache<T>),它使用Func<Task<T>>作为参数.

我还有一个变量cache,用户可以将其设置为truefalse.

如果是true,则应调用上述方法,并且应将我的task作为参数传递,如果是false,则应执行我的task,而不将其作为方法的参数.

我的最终结果将是这样的:

Func<Task<?>> fetch = () => _entityManager.UseRepositoryAsync(async (repo) =>
{
     IEnumerable<Entity> found = //... Get from repository
     return new { Data = found.ToList() };
}

return await (cache ? UseApplicationCache(fetch) : fetch());

解决方案

如何明确声明此类型

您不能.匿名类型没有名称,因此无法明确提及.

如果您创建通用帮助器方法,则可以推断出匿名类型.就您而言,您可以执行以下操作:

static Func<TAnon> InferAnonymousType<TAnon>(Func<TAnon> f)
{
  return f;
}

您可以这样做:

var fetch = InferAnonymousType(() => _entityManager.UseRepositoryAsync(async (repo) =>
{
     IEnumerable<Entity> found = //... Get from repository
     return new { Data = found.ToList() };
}
));

return await (cache ? UseApplicationCache(fetch) : fetch());

将自动推断TAnon的值".

Let's suppose I have the following task:

var task = _entityManager.UseRepositoryAsync(async (repo) => 
{
    IEnumerable<Entity> found = //... Get from repository

    return new 
    {
        Data = found.ToList()
    };
}

What is the type of task?

Actually, it turns out to be: System.Threading.Tasks.Task<'a>,

where 'a is anonymous type: { List<object> Data }

How can I explicitly state this type without using var?

I have tried Task<a'> task = ... or Task<object> task = ... but can't manage it to compile.

Why do I need to do this?

I have a method (UseApplicationCache<T>), that takes a Func<Task<T>> as a parameter.

I also have a variable cache that the user might set to true or false.

If true, the above said method should be called and my task should be passed as argument, if false, I should execute my task without giving it as an argument to the method.

My end result would be something like this:

Func<Task<?>> fetch = () => _entityManager.UseRepositoryAsync(async (repo) =>
{
     IEnumerable<Entity> found = //... Get from repository
     return new { Data = found.ToList() };
}

return await (cache ? UseApplicationCache(fetch) : fetch());

解决方案

How can I explicitly state this type

You cannot. An anonymous type has no name, hence cannot be explicitly mentioned.

The anonymous type can be inferred if you create a generic helper method. In your case, you can do:

static Func<TAnon> InferAnonymousType<TAnon>(Func<TAnon> f)
{
  return f;
}

With that you can just do:

var fetch = InferAnonymousType(() => _entityManager.UseRepositoryAsync(async (repo) =>
{
     IEnumerable<Entity> found = //... Get from repository
     return new { Data = found.ToList() };
}
));

return await (cache ? UseApplicationCache(fetch) : fetch());

The "value" of TAnon will be automatically inferred.

这篇关于如何指定匿名对象作为通用参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆