[英]Use anonymous type in generic class
I have the following class with an Add mapper 我有以下类添加映射器
public class OrderMapper<T> {
public OrderMapper<T> Add<TKey>(Expression<Func<T, TKey>> expression, String name) {
}
}
which I use as follows: 我使用如下:
OrderMapper<Post> mapper = new OrderMapper<Post>().Add(x => x.Created, "created");
The problem is that sometimes I have the following: 问题是有时候我有以下几点:
posts.SelectMany(x => x.Category, (Post, Category) => new { Post, Category });
Which returns a list of anonymous types: 其中返回匿名类型列表:
var objects = new { Post post, Category category }
I am not sure how to create an OrderMapper of this type ... 我不知道如何创建这种类型的OrderMapper ...
I think maybe I need to change my class method to an extension like: 我想也许我需要将我的类方法更改为如下的扩展名:
OrderMapper mapper = objects.Add(...).Add(...)
Could someone tell me the best way to do this? 有人能告诉我最好的方法吗?
You can use a Create
method for the OrderMapper
class : 您可以为OrderMapper
类使用Create
方法:
public class OrderMapper
{
public static OrderMapper<T> CreateMapperFor<T>(IEnumerable<T> typeProvider)
{
return new OrderMapper<T>();
}
}
And, use it as such : 并且,使用它:
var objects = Enumerable.Repeat(new { /* ... */ }, 1);
var mapper = OrderMapper.CreateMapperFor(objects)
.Add(/*...*/)
.Add(/*...*/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.