C# 如何从基类访问派生类中的属性

C# 如何从基类访问派生类中的属性,c#,properties,C#,Properties,基本上,我有一个设置,我需要访问派生类中的成员,同时代码仍然驻留在基类中,例如 public class DerivedClass : BaseClass<DerivedClass>{ DerivedClass() { // Set the value to 'test' here } } public class BaseClass<T> where T : class, new(){ public int test

基本上,我有一个设置,我需要访问派生类中的成员,同时代码仍然驻留在基类中,例如

public class DerivedClass : BaseClass<DerivedClass>{
     DerivedClass()
    {
        // Set the value to 'test' here
    }
}

public class BaseClass<T> where T : class, new(){
    public int test {get; set;}

    BaseClass()
    {
       // Use the value that DerivedClass set for 'test'
    }
}
公共类派生类:基类{
派生类()
{
//在此处将值设置为“test”
}
}
公共类基类,其中T:class,new(){
公共int测试{get;set;}
基类()
{
//使用DerivedClass为“测试”设置的值
}
}

如果答案非常明显,我深表歉意。

您可以直接访问测试。范例

public class Derived : Test
{
    public Derived()
    {
        _test = 5;
    }
}

public class Test
{
    public int _test;

    public int GetTest()
    {
         return _test;
    }
}

var obj = new Derived();
var test = obj.GetTest(); // returns 5

你是说
test
不起作用吗?基类怎么知道有派生类呢?我实际上还没有尝试过,我在代码中使用了一个单例,我试图通过它调用它,它使用一个类型来初始化,我甚至没有想过仅仅通过类来调用它。很抱歉问了这么愚蠢的问题。谢谢,我向约翰解释了我的回答