2014-09-10 126 views
-1

我需要访问基类类属性,它是来自已分类类的列表。下面是例子,我需要这个单元测试。从派生类c访问基类(列表)属性#

class child : List<Parent> 
{ 
    //this class is empty. 
    } 

class Parent 
{ 
     public List<Seller> Seller 
    { 
     get; 
     set; 
    } 

    public string Id 
    { 
     get; 
     set; 
    } 

}

我不能够访问父类的任何属性。请帮忙。

单元测试代码

[Test] 
public class test() 
{ 
    child a = new child(); 
    a. // not showing any properties of parent 
} 
+2

孩子从'List '继承,而不是'Parent'。所以这是一个“集合”。你必须做一些像'foreach(var parent in a){var id = parent.Id;}' – 2014-09-10 10:15:24

+0

我不会在这种情况下得到parent.id – user3660473 2014-09-10 10:19:37

+0

你是什么意思?如果你不在'List '中添加任何'Parent'(做一些像'a.Add(new Parent {Id = 2})'),当然你不会有任何东西...... – 2014-09-10 10:29:46

回答

0

,如果你想获得a.IDa.Seller必须声明:

class child :Parent 
{ 
    //this class is empty. 
} 
0

如果从List<T>派生你将继承它的能力,而不是从能力T。你可以做的是访问列表中的一个T来访问它的属性。

public class Parent 
{ 
    public bool IAmAParent { get; set; } 
} 

public class Child : List<Parent> 
{ } 

var c = new Child(); 
c[0].IAmAParent = true; 

从我所看到的我有这种感觉,你对你需要的继承感到困惑。如果您需要访问ParentChild中的属性,那么它应该继承Parent,然后放入列表中,而不是相反。

public class Parent 
{ 
    public bool IAmAParent { get; set; } 
} 

public class Child : Parent 
{ } 

var c = new List<Child>(); 
c[0].IAmAParent = true; 
+0

已经尝试过这种方式,但c [0]。 //不显示任何属性 – user3660473 2014-09-10 10:23:38

+0

请注意,列表必须包含至少一个项目,以便上面的代码可以工作。关于intellisense不工作,你可能会遇到一些编译问题,无法构建基类。代码中是否存在其他错误? – samy 2014-09-10 10:56:03