简体   繁体   English

从虚拟方法访问派生类

[英]Access derived class from virtual method

Though many questions on this topic exist, I am unable to find (yet) a satisfying solution: 尽管存在有关此主题的许多问题,但我仍无法找到令人满意的解决方案:

Is it possible (and if yes, how?) to access a derived class from the base virtual method? 是否可以(如果是,怎么办?)从基虚拟方法访问派生类?


Let's imagine I have the following classes: 假设我有以下课程:

public class parent_class
{
    public virtual string common_method () {
        dynamic child =  /* something to access the derived class */ ;

        if (child == null)
            return typeof(parent_class).FullName;
        else
            return child.GetType().FullName;
    }   
}

public class child_class1 : parent_class {}
public class child_class2 : parent_class {}
public class child_class3 : parent_class {}

Is it possible to execute the following code (and getting the correct result)? 是否可以执行以下代码(并获得正确的结果)?

parent_class p = new parent_class();
child_class1 c1 = new child_class1();
child_class2 c2 = new child_class2();
child_class3 c3 = new child_class3();

System.Console.WriteLine(p.common_method()); // result:  'parent_class'
System.Console.WriteLine(c1.common_method()); // result:  'child_class1'
System.Console.WriteLine(c2.common_method()); // result:  'child_class2'
System.Console.WriteLine(c3.common_method()); // result:  'child_class3'

EDIT: After reading the comments and replies I have to add the following points: 编辑:阅读评论和答复后,我必须添加以下几点:

  • I have about 300 different "child classes", so overriding is not an option 我大约有300种不同的“子类”,因此不能选择覆盖
  • I do not want to print the derived class' name - It was just an example 我不想打印派生类的名称-这只是一个示例
  • @Siamak Ferdos: I tried the this -keyword, but it somehow did not work as I intended. @Siamak Ferdos:我尝试了this -keyword,但是它以某种方式没有按我的预期工作。

Yes you can do it by 'this' keyword simply: 是的,您可以通过'this'关键字简单地做到这一点:

public class parent_class
    {
        public virtual string common_method()
        {
            //dynamic child =  /* something to access the derived class */ ;

            if (this.GetType() == typeof(parent_class))
                return typeof(parent_class).FullName;
            else
                return this.GetType().FullName;
        }
    }

    public class child_class1 : parent_class { }
    public class child_class2 : parent_class { }
    public class child_class3 : parent_class { }

If I understand your question correctly, you want to print the classes full name via a common base class method. 如果我正确理解了您的问题,则希望通过通用的基类方法打印类的全名。 At runtime, the actual instantiated type name is what GetType().FullName will produce, not the type of the base class. 在运行时, 实际的实例化类型名称是GetType().FullName将产生的内容,而不是基类的类型。

This can be tested with a simple example: 可以用一个简单的示例进行测试:

void Main()
{
    var parent = new Parent();
    var child = new Child();

    Console.WriteLine(parent.GetName());
    Console.WriteLine(child.GetName());
}

public class Parent
{
    public string GetName()
    {
        return this.GetType().FullName; 
    }
}

public class Child : Parent
{
}

Yields: 产量:

UserQuery+Parent
UserQuery+Child

Where UserQuery is the defined namespace. 其中UserQuery是定义的名称空间。

There is no need for the method to be virtual or overriden in the derived class for this to work. 不需要在派生类中对该方法进行虚拟化或重写该方法即可。

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

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