可以';由于泛型类型推理的局限性,没有使用泛型来改进API
本文关键字:泛型 API 局限性 推理 泛型类型 可以 | 更新日期: 2023-09-27 17:58:14
我有一个类,它被设计成接受任何类型的对象的集合并创建它的导出(例如Excel电子表格)。我可以根据自己的选择提供列名和宽度:
以下是课程摘要:
public class ObjectExporter
{
public string Export<T>(IEnumerable<T> objects, IEnumerable<ColumnDefinition> columnDefinitions)
{
//Iterate through list of ColumnDefinitions.
//Output value of the property in each object...
//...whose name is equal to the PropertyName of the current ColumnDefinition
}
}
这是ColumnDefinition类:
public class ColumnDefinition {
public string ColumnName { get; set; }
public string PropertyName { get; set; }
public int Width { get; set; }
public ColumnDefinition(string columnName, string propertyName, int width)
{
ColumnName = columnName;
PropertyName = propertyName;
Width = width;
}
}
这里有一个用法示例:
private void TestObjectExporter()
{
ObjectExporter objectExporter = new ObjectExporter();
//Note that the following variable changeRequests is a collection of anonymous type
var changeRequests = ChangeRequestRepository.All.Take(5).Select(x => new { x.ChangeRequested, x.DateCreated, x.CreatedBy.Username });
Response.Write(objectExporter.Export(changeRequests, new List<ColumnDefinition>()
{
new ColumnDefinition("Change Requested", "ChangeRequested", 12),
new ColumnDefinition("Date Created", "DateCreated", 12),
new ColumnDefinition("Created By", "Username", 12)
}));
}
ObjectExporter可以处理匿名类型的集合,这一点至关重要。我想修改ObjectExporter和ColumnDefinition类,使其使用如下强类型语法(注意如何指定属性名称):
Response.Write(objectExporter.Export(changeRequests, new List<ColumnDefinition>()
{
new ColumnDefinition("Change Requested", x => x.ChangeRequested, 12),
new ColumnDefinition("Date Created", x => x.DateCreated, 12),
new ColumnDefinition("Created By", x => x.Username, 12)
}));
我相信这样做的方法是创建一个ColumnDefinition<T>
类。然而,我找不到让编译器推断IEnumerable<ColumnDefinition<T>>
参数中使用的T
与IEnumerable<T>
参数中使用相同的方法。这意味着我不能再将类与匿名类型的集合一起使用,因为我不能显式指定泛型类型参数。
有人能想出办法吗?
在列定义类中,您可以执行以下操作:
public class ColumnDefinition<T>
{
public ColumnDefinition(string displayName, Expression<Func<T, string>> propertyExpression, int width)
{
}
}
您的对象导出器签名也会更改:
public string Export<T>(IEnumerable<T> items, IEnumerable<ColumnDefinition<T>> columns)
{
}
然后,T的类型应该从传递的枚举值中推断出来,因此第二个参数应该是IEnumerable>,这意味着ColumnDefinition类推断出类型T,然后构造函数中的表达式拾取T。
编辑:或者:
将您的Enumerable包装到另一个类中,然后调用它上的方法。
public class ObjectWrapper<T> : IDisposable
{
public IEnumerable<T> Items { get; protected set; }
public IEnumerable<ColumnDefinition> Definitions { get; set; }
public ObjectWrapper(IEnumerable<T> source)
{
Items = source;
Definitions = new List<ColumnDefinition>();
}
public void AddColumnDefinition(string name, Expression<Func<T, string>> propertyExpression, int width)
{
/* Add Column Definition with Expression Data */
}
}
如果不使用一些技巧来强制进行类型推理,这将有点棘手。这样的怎么样?
Response.Write(objectExporter.Export(changeRequests, new[]
{
changeRequests.GetColumnDef("Change Requested", x => x.ChangeRequested, 12),
changeRequests.GetColumnDef("Date Created", x => x.DateCreated, 12),
changeRequests.GetColumnDef("Created By", x => x.Username, 12)
}));
// ...
public class ColumnDefinition<T>
{
public string ColumnName { get; private set; }
public Func<T, object> Selector { get; private set; }
public int Width { get; private set; }
public ColumnDefinition(string columnName, Func<T, object> selector, int width)
{
ColumnName = columnName;
Selector = selector;
Width = width;
}
}
public static class ColumnDefinitionHelper
{
public static ColumnDefinition<T> GetColumnDef<T>(this IEnumerable<T> source,
string columnName, Func<T, object> selector, int width)
{
return new ColumnDefinition<T>(columnName, selector, width);
}
}
public class ObjectExporter
{
public string Export<T>(
IEnumerable<T> objects, IEnumerable<ColumnDefinition<T>> columnDefinitions)
{
// ...
}
}
如果您的更改不是真正的泛型,请不要使用泛型。
如果不能用在t中实现的Interface实例替换t,就不要使用泛型。这没有道理。