简体   繁体   English

返回父类中的子类型

[英]Returning child type in Parent class

      public abstract class Base
      {
         public Base ClassReturn()
        {
            return this;
        }
      }

Is there possibility to return child type that invoked ClassReturn method? 是否有可能返回调用ClassReturn方法的子类型? I've done that in extension method: 我已经在扩展方法中做到了:

public static T ClassReturn<T>(this T obj) where T : Base
        {
            return (T) obj.ClassReturn();
        }

But I want to embeed it in Base class instead of extension method. 但我想将其嵌入到基类中,而不是扩展方法中。 Is there possibility to do that with generics? 有可能使用泛型吗?

I will copy my comment which describes what I want to achieve: 我将复制我的评论,描述我想要实现的目标:

I need something similiar to builder pattern and I have different classes that depending on previous operations do something else, now I want to have a similiar functionality in every of them and when I use it I lose object type. 我需要一些类似于生成器模式的东西,并且我有不同的类,这些类根据以前的操作执行其他操作,现在我想在每个类中都具有类似的功能,并且当我使用它时,我会丢失对象类型。 So my solution is either implement that functionality multiple times in every class or create extension method. 因此,我的解决方案是在每个类中多次实现该功能,或者创建扩展方法。 But I always thought when it is possible to make extension method for class then I can embeed that in class, but as I see it is not possible. 但是我一直认为,只要有可能为类创建扩展方法,就可以将其嵌入到类中,但是我认为这是不可能的。

Full example: 完整示例:

 public class Child1 : Base
        {
            public Child1 Operation1()
            {
                Console.WriteLine("operation1");
                return this;
            }
        }

        public class Child2 : Base
        {
            public Child2 Operation2()
            {
                Console.WriteLine("operation2");
                return this;
            }
        }



        static void Main(string[] args)
        {
            Child1 ch = new Child1();
            ch.Operation1().Operation1().ClassReturn().Operation1()
        }

I can't use Operation1 after ClassReturn if I don't use extension method. 如果不使用扩展方法,则无法在ClassReturn之后使用Operation1。

Try this one: 试试这个:

public abstract class Base<T> where T: Base<T>
{
    public T ClassReturn
    {
        get { return (T)this; }
    }
}

public class Child1 : Base<Child1>
{
}

public class Child2 : Base<Child2>
{
}

From your question and your comments, what you are trying to achieve is not possible directly from the type system. 从您的问题和评论中,您无法尝试直接从类型系统中实现什么。 By returning an instance of Base you are specifically saying that all you are interested is that you have something that derives from Base, but that the specific class doesn't matter. 通过返回Base的实例,您特别要说的是,您感兴趣的只是您有一些从Base派生的东西,但是特定的类无关紧要。 Statically, the compiler no longer has the information it needs to perform a cast. 静态地,编译器不再具有执行强制转换所需的信息。

If you are trying to get the original type back statically, then you have to supply the information to the compiler, and in this case you can't guarantee that you have the correct information. 如果您试图静态地恢复原始类型,则必须将信息提供给编译器,在这种情况下,您不能保证您拥有正确的信息。 In the example below, the instance is created from derived type A but attempted to be cast to derived type B through the extension, the compiler will allow the code to compile, but you'll get an exception at runtime. 在下面的示例中,实例是从派生类型A创建的,但是尝试通过扩展将其强制转换为派生类型B ,编译器将允许代码进行编译,但是您将在运行时遇到异常。

public class A : Base { }
public class B : Base { }

public static class BaseExtensions
{
    public static T GetAsT<T>(this Base base)  where T: Base
    {
       return (T)base;
    }
}


public static void Main() 
{
    Base obj = new A();
    B b = obj.BaseAsT<B>(); // This compiles but causes an exception
}

You should look up the Liscov Substitution Principle to get information on how to properly work with base and derived classes in the system as a whole, and then write up a question dealing specifically with the result you are trying to achieve. 您应该查阅《 Liscov替代原理》,以获取有关如何在整个系统中正确使用基类和派生类的信息,然后编写一个专门解决您要达到的结果的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM