如何指定匿名对象作为通用参数? [英] How to specify anonymous object as generic parameter?
问题描述
假设我有以下任务:
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
,用户可以将其设置为true
或false
.
如果是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屋!