关于泛型、继承和链接的棘手问题
对于上下文 - 阅读本文。
问题:
class Program
{
static void Main()
{
var b = new bar();
b.buzz().fizz().buzz().fizz(); //cool
// ^FAIL!!!...<------------------------------------
Console.ReadLine();
}
}
public class foo
{
public foo fizz() { return this; }
}
public class bar : foo
{
public bar buzz()
{
return this;
}
}
解决方案:
class Program
{
static void Main()
{
var b = new bar();
b.buzz().fizz().buzz().fizz(); //cool stuff
Console.ReadKey();
}
}
public static class fooExtensions
{
public static T fizz<T>(this T t) where T : foo
{ return t; }
}
public class foo{}
public class bar : foo
{
public bar buzz()
{
return this;
}
}
这是一种如何“模仿”基类方法的技术,该方法能够返回派生类型(否则我的 b 无法再次调用嗡嗡声())。
更进一步,使 foo/bar 通用(这仍然可以正常工作):
class Program
{
static void Main()
{
var b = new bar<buzz>();
b.buzz().fizz().buzz().fizz(); //cool
Console.ReadLine();
}
}
public static class fooExtensions
{
public static T fizz<T>(this T t) where T : foo<buzz>
{ return t; }
}
public class buzz { public string name { get; set;} }
public class foo<T> where T : buzz
{}
public class bar<T> : foo<T> where T : buzz
{
public bar<T> buzz()
{
return this;
}
}
问题是 -
如何将 lambda 传递给了解 tbuzz
及其属性的 fizz
方法,而无需显式传递类型参数。
可能反映我正在寻找的内容的损坏代码:
class Program
{
static void Main()
{
var b = new bar<buzz>();
b.buzz().fizz(x=>x.name).buzz().fizz(); //not cool anymore
// ^FAIL!!!<---------------------------------
Console.ReadLine();
}
}
public static class fooExtensions
{
//NO IDEAS WHAT TO WRITE BELOW!!!
public static T fizz<T, Tbuzz>(this T t,
Func<Tbuzz, object> superlambda)
where T : foo<buzz>
where Tbuzz : buzz
{
return t;
}
}
public class buzz { public string name { get; set;} }
public class foo<T> where T : buzz
{}
public class bar<T> : foo<T> where T : buzz
{
public bar<T> buzz()
{
return this;
}
}
想知道这是否可能。如果不是——为什么?
理论上 - foo
知道下面有嗡嗡声
。
是否有其他方法如何创建基本方法或模仿它来支持此类的链接?
For context - read this.
Problem:
class Program
{
static void Main()
{
var b = new bar();
b.buzz().fizz().buzz().fizz(); //cool
// ^FAIL!!!...<------------------------------------
Console.ReadLine();
}
}
public class foo
{
public foo fizz() { return this; }
}
public class bar : foo
{
public bar buzz()
{
return this;
}
}
Solution:
class Program
{
static void Main()
{
var b = new bar();
b.buzz().fizz().buzz().fizz(); //cool stuff
Console.ReadKey();
}
}
public static class fooExtensions
{
public static T fizz<T>(this T t) where T : foo
{ return t; }
}
public class foo{}
public class bar : foo
{
public bar buzz()
{
return this;
}
}
This is a technique how to 'mimic' method of base class which is able to return back derived type (otherwise my b couldn't call buzz() again).
Going further and making foo/bar generic (this will still work fine):
class Program
{
static void Main()
{
var b = new bar<buzz>();
b.buzz().fizz().buzz().fizz(); //cool
Console.ReadLine();
}
}
public static class fooExtensions
{
public static T fizz<T>(this T t) where T : foo<buzz>
{ return t; }
}
public class buzz { public string name { get; set;} }
public class foo<T> where T : buzz
{}
public class bar<T> : foo<T> where T : buzz
{
public bar<T> buzz()
{
return this;
}
}
And the question is -
how to pass lambda to fizz
method that knows about tbuzz
and it's properties without passing type parameter/s explicitly.
Broken code that might reflect what i'm looking for:
class Program
{
static void Main()
{
var b = new bar<buzz>();
b.buzz().fizz(x=>x.name).buzz().fizz(); //not cool anymore
// ^FAIL!!!<---------------------------------
Console.ReadLine();
}
}
public static class fooExtensions
{
//NO IDEAS WHAT TO WRITE BELOW!!!
public static T fizz<T, Tbuzz>(this T t,
Func<Tbuzz, object> superlambda)
where T : foo<buzz>
where Tbuzz : buzz
{
return t;
}
}
public class buzz { public string name { get; set;} }
public class foo<T> where T : buzz
{}
public class bar<T> : foo<T> where T : buzz
{
public bar<T> buzz()
{
return this;
}
}
Wondering if that's possible. And if not - why?
Theoretically - foo<T>
knows that there's a buzz
underneath.
Are there any other approach how to create base method or mimic it that supports chaining for such a classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你想要实现的目标是不可能的。理论上,您需要的是某种约束,例如:
意味着 TBuzz 需要是 T 的内部泛型类型。据我所知,这是不可能的。
I don't think what you're trying to accomplish is possible. Theoretically what you would need is some kind of constraint like:
meaning TBuzz needs to be the type that's the inner generic type of T. This isn't possible as far as I know.
这个?
this?
起初我以为我在某种画眉取得了进展组合器
但这似乎是一个死胡同。无论如何发布它,以防你可以改进它。
At first I thought I made progress with a kind of Thrush combinator
but that seems to be a dead end. Posting it anyway in case you can improve it.