Why a property inherited from interface became virtual?
up vote
9
down vote
favorite
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?
c# reflection
add a comment |
up vote
9
down vote
favorite
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?
c# reflection
Finally a decent question
– T.S.
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago
add a comment |
up vote
9
down vote
favorite
up vote
9
down vote
favorite
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?
c# reflection
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?
c# reflection
c# reflection
asked 3 hours ago
runerback
768
768
Finally a decent question
– T.S.
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago
add a comment |
Finally a decent question
– T.S.
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago
Finally a decent question
– T.S.
2 hours ago
Finally a decent question
– T.S.
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
As from remarks section of MS docs:
A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method
virtual final
If you need to determine whether this method is overridable then checking IsVirtual
is not enough and you need to also check that IsFinal
is false.
Here is an extension method that do this check:
public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
As from remarks section of MS docs:
A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method
virtual final
If you need to determine whether this method is overridable then checking IsVirtual
is not enough and you need to also check that IsFinal
is false.
Here is an extension method that do this check:
public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;
add a comment |
up vote
9
down vote
accepted
As from remarks section of MS docs:
A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method
virtual final
If you need to determine whether this method is overridable then checking IsVirtual
is not enough and you need to also check that IsFinal
is false.
Here is an extension method that do this check:
public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;
add a comment |
up vote
9
down vote
accepted
up vote
9
down vote
accepted
As from remarks section of MS docs:
A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method
virtual final
If you need to determine whether this method is overridable then checking IsVirtual
is not enough and you need to also check that IsFinal
is false.
Here is an extension method that do this check:
public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;
As from remarks section of MS docs:
A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method
virtual final
If you need to determine whether this method is overridable then checking IsVirtual
is not enough and you need to also check that IsFinal
is false.
Here is an extension method that do this check:
public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;
edited 32 mins ago
Alexei Levenkov
83.4k888129
83.4k888129
answered 3 hours ago
vasily.sib
1,7411716
1,7411716
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492538%2fwhy-a-property-inherited-from-interface-became-virtual%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Finally a decent question
– T.S.
2 hours ago
upovted for clarity of question
– Ehsan Sajjad
2 hours ago