C# 如何在同一基类的方法中获取所有属性?

C# 如何在同一基类的方法中获取所有属性?,c#,inheritance,properties,C#,Inheritance,Properties,实际上,我希望访问方法中基类的属性,而不是直接实例化该对象。以下是我正在处理的代码: public class Test { public static void Main() { drivedclass obj = new drivedclass(); obj.DoSomething(); } } public class drivedclass : baseclass { public void DoSomething()

实际上,我希望访问方法中基类的属性,而不是直接实例化该对象。以下是我正在处理的代码:

public class Test
{
    public static void Main()
    {
        drivedclass obj = new drivedclass();
        obj.DoSomething();
    }
}

public class drivedclass : baseclass
{
    public void DoSomething()
    {
        LoadSomeThing();
    }
}

public class baseclass
{
    public string property1
    {
        get;
        set;
    }
    public string property2
    {
        get;
        set;
    }
    public void LoadSomeThing()
    {
        //here I want to access values of all properties
    }
}

我想知道是否有一种方法,我可以访问同一类的方法中的属性,并且该类是基类。

您可以直接使用
property1
property2


但是,请注意,在
LoadSomeThing()
中,您将无法访问
drivedlcass
的任何属性,因为基类无法根据定义查看其派生类的属性。

您可以直接使用
property1
property2


但是,请注意,在
LoadSomeThing()
中,您将无法访问
drivedlcass
的任何属性,因为基类无法根据定义查看其派生类的属性。

您可以通过反射访问它们,但这不是“正常”方式

foreach(PropertyInfo prop in this.GetType().GetProperties())
{
    prop.SetValue(this, newValue);
}

如果要使其“更干净”,应将属性设置为虚拟属性。

您可以通过反射访问它们,但这不是“正常”方式

foreach(PropertyInfo prop in this.GetType().GetProperties())
{
    prop.SetValue(this, newValue);
}

如果要使其“更干净”,应将属性设置为虚拟属性。

使用以下方法枚举所有属性值:

        public void EnumerateProperties()
    {
        var propertiesInfo = this.GetType().GetProperties();
        foreach (var propertyInfo in propertiesInfo)
        {
            var val = propertyInfo.GetValue(this, null);
        }
    }

使用以下方法枚举所有属性值:

        public void EnumerateProperties()
    {
        var propertiesInfo = this.GetType().GetProperties();
        foreach (var propertyInfo in propertiesInfo)
        {
            var val = propertyInfo.GetValue(this, null);
        }
    }

这个问题还不清楚,但如果您希望访问您的属性,它们在基类和派生类中都存在。因此,如果您在主类测试中执行了
s=obj.property2
,则应该可以使用

public class Test {
    public static void Main( ) {
      drivedclass obj = new drivedclass( );
      obj.DoSomething( );
      string s = obj.property2 ;
    }
  }

这个问题还不清楚,但如果您希望访问您的属性,它们在基类和派生类中都存在。因此,如果您在主类测试中执行了
s=obj.property2
,则应该可以使用

public class Test {
    public static void Main( ) {
      drivedclass obj = new drivedclass( );
      obj.DoSomething( );
      string s = obj.property2 ;
    }
  }

你可以把它说清楚:

public class DerivedClass : BaseClass
{
    public string Property3
    { get; set; }

    public void DoSomething ()
    {
        LoadSomeThing();
    }

    public override void LoadSomeThing ()
    {
        base.LoadSomeThing();
        Console.WriteLine(Property3);
    }
}

public class BaseClass {
    public string Property1
    { get; set; }
    public string Property2
    { get; set; }

    public virtual void LoadSomeThing()
    {
        Console.WriteLine(Property1);
        Console.WriteLine(Property2);
    }
}

你可以把它说清楚:

public class DerivedClass : BaseClass
{
    public string Property3
    { get; set; }

    public void DoSomething ()
    {
        LoadSomeThing();
    }

    public override void LoadSomeThing ()
    {
        base.LoadSomeThing();
        Console.WriteLine(Property3);
    }
}

public class BaseClass {
    public string Property1
    { get; set; }
    public string Property2
    { get; set; }

    public virtual void LoadSomeThing()
    {
        Console.WriteLine(Property1);
        Console.WriteLine(Property2);
    }
}


您可以简单地尝试:this.property1

您可以简单地尝试:this.property1

问题目前真的不清楚只要尝试一下,您就会得到一堆信息。问题目前真的不清楚只要尝试一下,您就会得到一堆信息。您建议类进行反射以访问它自己的
public
属性?@Rotem:访问公共属性时不必使用反射?从您的问题中不清楚您是否要枚举所有类属性。您建议类使用反射来访问其自己的
public
属性?@Rotem:访问公共属性时不必使用反射吗?从您的问题中不清楚你想枚举所有类属性的问题。很高兴你添加了一条注释,说明这不是正常的方式:)我感觉OP只是在学习继承的原理,反射并不是他们真正想要的。你建议类进行反射以访问它自己的
公共
属性?@Rotem为什么不是吗?我只展示了它可以通过反射来实现,但是我最好在没有反射的情况下实现它。如果他正在进行类似序列化的操作,也会编写派生类的属性。可以这样做。否则,他应该重新考虑他的对象模型。他应该实现虚拟/抽象属性。@JeroenvanLangen再次阅读了这个问题:)您建议对对象进行反射以访问其自己的属性。@Rotem很好的注意:),有些人将“//这里我想访问所有属性的值”解释为包括派生类属性在内的所有属性。:)很高兴你补充了这不是正常的方式:)我感觉OP只是在学习继承的原理,反射并不是他们真正想要的。你建议反射让一个类访问它自己的
public
properties?@Rotem为什么不?),我只展示了反射可以完成,但我最好是不加思考地实施它。如果他正在进行类似序列化的操作,也会编写派生类的属性。可以这样做。否则,他应该重新考虑他的对象模型。他应该实现虚拟/抽象属性。@JeroenvanLangen再次阅读了这个问题:)您建议对对象进行反射以访问其自己的属性。@Rotem很好的注意:),有些人将“//这里我想访问所有属性的值”解释为包括派生类属性在内的所有属性。:)