使用派生类在类的当前实例中创建当前类的列表。 [英] Create a list of the current class inside the current instance of the class whe using a derived class.

查看:47
本文介绍了使用派生类在类的当前实例中创建当前类的列表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

请考虑以下代码。 我正在使用基础课程 以及派生类。 我想要做的是创建一个我的列表衍生类作为示例显示 如何确定派生类类型,以便我可以在列表中声明
< derivedclass>

Consider the following code.  I am using a Base Class  along with a Derived Class.  What I would like to do is create a list of my  derived class as the example shows   How can I determine the derived class type so I can declare the list<derivedclass>

我希望保持这种通用性,以便我可以使用任何派生类。

I would like to keep this generic so I can use any derived class.

感谢您的帮助。

using System.Collections.Generic;

namespace AbstractSandbox
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            TheDerivedClass1 n = new TheDerivedClass1();
            n.SomeString = "Test";
            n.aNumber = 1;

            n.method_one("xyz");
        }
    }

    public class TheDerivedClass1 : TheBaseClass
    {
        private int _aNumber;

        public int aNumber
        {
            get { return _aNumber; }
            set { _aNumber = value; }
        }
    }

    public class TheDerivedClass2 : TheBaseClass
    {
        private double _aNumber;

        public double aNumber
        {
            get { return _aNumber; }
            set { _aNumber = value; }
        }
    }


    public class TheBaseClass
    {
        private string _someString;

        public string SomeString
        {
            get { return _someString; }
            set { _someString = value; }
        }

        public TheBaseClass()
        {
            var alist = new List<string>();


            // Is it possible to get the type of the instance that used the base class and plug 
            // it into a list that is inside the base class so I can make a list of the derived
            // classes.  I would like this generic so any derived class can be used.

            var blist = new List<Want_the_derived_class_here>();

        }

        public void method_one(string somthing)
        {

        }

    }
}

推荐答案

    //是否可以获取使用基类的实例类型并插入


           
//将它放入基类内的列表中,这样我就可以列出派生的

           
//类。 我希望这个通用,所以可以使用任何派生类。



      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
var blist
= new
列表 < Want_the_derived_class_here >();

  // Is it possible to get the type of the instance that used the base class and plug
           
// it into a list that is inside the base class so I can make a list of the derived
           
// classes.  I would like this generic so any derived class can be used.

           
var blist = new List<Want_the_derived_class_here>();

---- -------------------------------------------------- --------------------------- 没有意义,它离开
上限,我建议你放弃这个。

---------------------------------------------------------------------------------It makes no sense, it's off the ceiling, and I suggest that you abandon this.


这篇关于使用派生类在类的当前实例中创建当前类的列表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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