关于仿制药,继承和链接棘手的问题 [英] Tricky question about generics, inheritance and chaining

查看:143
本文介绍了关于仿制药,继承和链接棘手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关背景 - 的阅读本

问题:

 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;
        }
    }

这是一个技术如何'模仿'的方法基类,它能够返回回派生类型(否则我b不能叫嗡嗡声()再次)。

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;
        }
    }





和问题是 -
如何通过lambda来香味,它知道方法 tbuzz ,它是没有过性类型参数/ S明确

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?

在理论上 - 富< T> 人都知道有一个嗡嗡声下方。

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?

推荐答案

我不认为你想做到的是什么可能。理论上你将需要某种像约束:

I don't think what you're trying to accomplish is possible. Theoretically what you would need is some kind of constraint like:

where TBuzz : T<inner>



意TBuzz必须是这样的内一般类型T的类型,这是不可能的据我所知。

meaning TBuzz needs to be the type that's the inner generic type of T. This isn't possible as far as I know.

这篇关于关于仿制药,继承和链接棘手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆