小编Onc*_*ler的帖子

C#对象继承

我试图在c#中创建一个基类,我可以扩展到子类.

例如:

public class ObjectsInTheSky 
{
    public string Size, Shape;
    public float Mass;
    public int DistanceFromEarth;
    public bool hasAtmosphere, hasLife;
    public enum ObjectTypes {Planets,Stars,Moons}

    public ObjectsInTheSky( int id ) 
    {
        this.Load( id );
    }
    public void Load( int id) 
    {
        DataTable table = Get.DataTable.From.DataBase(id);

        System.Reflection.PropertyInfo[] propInfo = this.GetType().GetProperties();
        Type tp = this.GetType();
        foreach (System.Reflection.PropertyInfo info in propInfo)
        {
            PropertyInfo p = tp.GetProperty(info.Name);
            try
            {
                if (info.PropertyType.Name == "String")
                {
                    p.SetValue(this, table.Rows[0][info.Name].ToString(), null);
                }
                else if (info.PropertyType.Name == "DateTime")
                {
                    p.SetValue(this, …
Run Code Online (Sandbox Code Playgroud)

c# inheritance base-class

3
推荐指数
1
解决办法
788
查看次数

标签 统计

base-class ×1

c# ×1

inheritance ×1