lundi 26 novembre 2018

Why a property inherited from interface became virtual?

Say I have one interface and two classes, one of the class implement that interface:

interface IAAA
{
    int F1 { get; set; }
}

class AAA1
{
    public int F1 { get; set; }
    public int F2 { get; set; }
}

class AAA2 : IAAA
{
    public int F1 { get; set; }
    public int F2 { get; set; }
}

In class AAA2, property F1 is 'inherited' (I'm not sure) from interface IAAA, then I use reflection to check whether a property is virtual:

Console.WriteLine("AAA1 which not implement IAAA");
foreach (var prop in typeof(AAA1).GetProperties())
{
    var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
    Console.WriteLine($@"{prop.Name} is{virtualOrNot} virtual");
}

Console.WriteLine("AAA2 which implemented IAAA");
foreach (var prop in typeof(AAA2).GetProperties())
{
    var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
    Console.WriteLine($"{prop.Name} is{virtualOrNot} virtual");
}

the output is:

AAA1 which not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implemented IAAA
F1 is virtual
F2 is not virtual

any reason for this?





Aucun commentaire:

Enregistrer un commentaire