gpt4 book ai didi

c# - 是否可以创建可以在 C# 中多次嵌套泛型类型的流畅接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:58 25 4
gpt4 key购买 nike

我正在尝试创建一个描述嵌套结构的 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>
{
}

如您所见,这是非常有限的,因为它只允许您深入到您定义的结构,并且您需要为每个更深的级别创建一个新结构。是否可以创建允许无限数量级别的类型(即,在没有相同相应数量类型的情况下,您可以根据需要选择任意数量的选择)。

最佳答案

是的,有点……可以或多或少地自动创建一些很长很深的嵌套类型。诀窍是你需要使用泛型类型推断,因为 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, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], uy2zelya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]

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, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], uy2zelya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]

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=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], uy2zelya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], uy2zelya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], uy2zelya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]

它甚至是类型安全的,这已经足够令人惊讶了。如果您添加必要的属性...

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com