Get properties from derived class in base class

前端 未结 7 2233
面向向阳花
面向向阳花 2021-02-09 07:02

How do I get properties from derived class in base class?

Base class:

public abstract class BaseModel {
    protected static readonly Dictionary

        
相关标签:
7条回答
  • 2021-02-09 07:10

    Another way to solve this issue by create virtual property in base class and override it to derived class.

    public class Employee
    {
        public virtual string Name {get; set;}
    }
    
    public class GeneralStaff
    {
        public override string Name {get; set;}
    }
    
    class Program
    {
            static void Main(string[] args)
            {
                Employee emp = new GeneralStaff();
                emp.Name = "Abc Xyz";
                //---- More code follows----            
            }
    }
    
    0 讨论(0)
  • 2021-02-09 07:21

    If you require that a derived class must implement a method or property, you should introduce that method or property into the base class as an abstract declaration.

    For example, for your Name property, you would add to the base class:

    public abstract string Name { get; set; }
    

    Then any derived classes must implement it, or be abstract classes themselves.

    Once you have added the abstract version of the Name property to the base class, you will be able to access it in the base class anywhere except in the base class's constructor.

    0 讨论(0)
  • 2021-02-09 07:26

    Scan your assembly for all inherited classes from BaseModel and create dictionary like this:

    Dictionary<Type, Dictionary<string, Func<BaseModel, object>>>
    
    0 讨论(0)
  • 2021-02-09 07:28

    If both classes are in the same assembly, you can try this:

    Assembly
        .GetAssembly(typeof(BaseClass))
        .GetTypes()
        .Where(t => t.IsSubclassOf(typeof(BaseClass))
        .SelectMany(t => t.GetProperties());
    

    This will give you all the properties of all the subclasses of BaseClass.

    0 讨论(0)
  • 2021-02-09 07:28
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TESTNEW
    {
       public abstract class BusinessStructure
        {
           public BusinessStructure()
           { }
    
           public string Name { get; set; }
       public string[] PropertyNames{
           get
           { 
                    System.Reflection.PropertyInfo[] Pr;
                    System.Type _type = this.GetType();
                    Pr = _type.GetProperties();
                    string[] ReturnValue = new string[Pr.Length];
                    for (int a = 0; a <= Pr.Length - 1; a++)
                    {
                        ReturnValue[a] = Pr[a].Name;
                    }
                    return ReturnValue;
           }
       }
    
    }
    
    
    public class MyCLS : BusinessStructure
       {
           public MyCLS() { }
           public int ID { get; set; }
           public string Value { get; set; }
    
    
       }
       public class Test
       {
           void Test()
           {
               MyCLS Cls = new MyCLS();
               string[] s = Cls.PropertyNames;
               for (int a = 0; a <= s.Length - 1; a++)
               {
                System.Windows.Forms.MessageBox.Show(s[a].ToString());
               }
           }
       }
    }
    
    0 讨论(0)
  • 2021-02-09 07:32

    If you must do literally fetch property of derived class from within base class, you can use Reflection, for example - like this...

    using System;
    public class BaseModel
    {
        public string getName()
        {
            return (string) this.GetType().GetProperty("Name").GetValue(this, null);
        }
    }
    
    public class SubModel : BaseModel
    {
        public string Name { get; set; }
    }
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                SubModel b = new SubModel();
                b.Name = "hello";
                System.Console.Out.WriteLine(b.getName()); //prints hello
            }
        }
    }
    

    This is not recommended, though, and you most probably should rethink your design like Matthew said.

    As for not throwing properties to your base classes -- you can try to decouple base and deriving classes into unrelated objects and pass them via constructors.

    0 讨论(0)
提交回复
热议问题