我正在尝试创建一个描述嵌套结构的Fluent API,因此,我想使用泛型来键入结果对象。我想做类似的事情:
var test = new Query<Entity>().Select(x => x.UniqueValue<string>())
.Select(x => x.UniqueValue<DateTime>())
.Select(x => x.UniqueValue<decimal>());
测试类型为:
Result<Entity, string, Result<Entity, DateTime, Result<Entity, decimal>>>
到目前为止,我有这样的代码:
class Program
{
static void Main(string[] args)
{
// test : ResultWithResult<Entity,string, Result<Entity, DateTime, ChildLessResult<Entity, decimal>>>
var test = new Query<Entity>().Select(x => x.UniqueValue<string>())
.Select(x => x.UniqueValue<DateTime>())
.Select(x => x.UniqueValue<decimal>());
}
}
public class Entity
{
public string Item1 { get; set; }
}
public class Aggregate<TEntity>
{
public ChildLessResult<TEntity, TReturnValue> UniqueValue<TReturnValue>()
{
return new ChildLessResult<TEntity, TReturnValue>();
}
}
public class Query<TEntity>
{
public ChildLessResult<TEntity, TReturnValue> Select<TReturnValue>(Func<Aggregate<TEntity>, ChildLessResult<TEntity, TReturnValue>> predicate)
{
var aggregator = new Aggregate<TEntity>();
return predicate(aggregator);
}
}
public class ChildLessResult<TEntity, TReturnValue>
{
public Result<TEntity, TReturnValue, TChild> Select<TChild>(Func<Aggregate<TEntity>, ChildLessResult<TEntity, TChild>> predicate)
{
return new Result<TEntity, TReturnValue, TChild>();
}
}
public class Result<TEntity, TReturnValue, TChild>
{
private ChildLessResult<TEntity, TChild> _child = new ChildLessResult<TEntity, TChild>();
public ResultWithResult<TEntity, TReturnValue, Result<TEntity, TChild, ChildLessResult<TEntity, TNewChild>>> Select<TNewChild>(Func<Aggregate<TEntity>, ChildLessResult<TEntity, TNewChild>> predicate)
{
return new ResultWithResult<TEntity, TReturnValue,
Result<TEntity, TChild, ChildLessResult<TEntity, TNewChild>>>();
}
}
public class ResultWithResult<TEntity, TReturnValue, TChild>
{
}
如您所见,这是非常有限的,因为它只允许您深入定义的结构,并且您需要为每个级别更深的新结构。是否有可能创建允许无限数量级别的类型(即,在没有相同数量的相应类型的情况下,您想要多少个Select)。
最佳答案
是的,有点……可以自动或多或少地创建一些很长很深的嵌套类型。诀窍是您需要使用通用类型推断,并且由于constructors don't do inference,您必须使用工厂方法。例如:
public class MyType<TData,TChild>
{
public MyType(TData data, TChild child)
{
}
}
public class MyTypeFactory
{
public static MyType<TData,TChild> Create<TData,TChild>(TData data, TChild child)
{
return new MyType<TData,TChild>(data, child);
}
public static MyType<TData,object> Create<TData>(TData data)
{
return new MyType<TData,object>(data, null);
}
}
public static class Program
{
static public void Main()
{
var grandchild = MyTypeFactory.Create( 12 );
var child = MyTypeFactory.Create( 13D, grandchild );
var parent = MyTypeFactory.Create( 14M, child) ;
var grandparent = MyTypeFactory.Create( 15F, parent );
Console.WriteLine( grandchild.GetType().FullName );
Console.WriteLine( child.GetType().FullName );
Console.WriteLine( parent.GetType().FullName );
Console.WriteLine( grandparent.GetType().FullName );
}
}
输出:
MyType'2 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]
MyType'2 [[System.Double,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089]],uy2zelya,版本= 0.0.0.0,文化=中性,PublicKeyToken =空]]
MyType'2 [[System.Decimal,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Double,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 ]],uy2zelya,版本= 0.0.0.0,文化=中性,PublicKeyToken =空]],uy2zelya,版本= 0.0.0.0,文化=中性,PublicKeyToken =空]]
MyType'2 [[System.Single,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Decimal,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Double,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[MyType'2 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture =中性,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089]],uy2zelya,版本= 0.0.0.0,文化=中性,PublicKeyToken = null]],uy2zelya版本= 0.0.0.0,文化=中性,PublicKeyToken =空]],uy2zelya,版本= 0.0.0.0,文化=中性,PublicKeyToken =空]]
它甚至是类型安全的,足够了。如果添加必要的属性...
public class MyType<TData,TChild>
{
protected readonly TChild _child;
protected readonly TData _data;
public MyType(TData data, TChild child)
{
_data = data;
_child = child;
}
public TChild Child
{
get
{
return _child;
}
}
public TData Data
{
get
{
return _data;
}
}
}
...这将起作用:
var grandchildValue = grandparent.Child.Child.Child.Data;
Console.WriteLine(grandchildValue); //12
Console.WriteLine(grandchildValue.GetType().Name); //int32
当然,您可以使用构建器模式:
public static class ExtensionMethods
{
static public MyType<TDataAdd,MyType<TDataIn,TResultIn>> AddColumn<TDataIn,TResultIn,TDataAdd>(this MyType<TDataIn,TResultIn> This, TDataAdd columnValue)
{
return MyTypeFactory.Create(columnValue, This);
}
}
var grandparent = MyTypeFactory.Create( 12 )
.AddColumn( 13D )
.AddColumn( 14M )
.AddColumn( 15F );
var grandchildValue = grandparent.Child.Child.Child.Data;
Console.WriteLine(grandchildValue); //12
Console.WriteLine(grandchildValue.GetType().FullName); //System.Int32
See my code on DotNetFiddle
附言这是为什么我们需要
var
关键字的一个很好的例子!否则,您将永远无法打字。关于c# - 是否可以创建可以在C#中多次嵌套泛型类型的流畅接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49764089/