Why does a property inherited from an interface become virtual?
up vote
30
down vote
favorite
Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implements IAAA
F1 is virtual
F2 is not virtual
Any reason for this?
c# reflection
add a comment |
up vote
30
down vote
favorite
Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implements IAAA
F1 is virtual
F2 is not virtual
Any reason for this?
c# reflection
add a comment |
up vote
30
down vote
favorite
up vote
30
down vote
favorite
Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implements IAAA
F1 is virtual
F2 is not virtual
Any reason for this?
c# reflection
Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implements IAAA
F1 is virtual
F2 is not virtual
Any reason for this?
c# reflection
c# reflection
edited Nov 27 at 13:48
Boann
36.5k1287120
36.5k1287120
asked Nov 27 at 3:58
runerback
18119
18119
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
28
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;
Your method also returns false for a method (or getter/setter of a property etc.) declaredsealed override
in C# (whether it implements an interface or not). Checking.IsVirtual
alone in these cases would yield true.
– Jeppe Stig Nielsen
Nov 27 at 14:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
28
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;
Your method also returns false for a method (or getter/setter of a property etc.) declaredsealed override
in C# (whether it implements an interface or not). Checking.IsVirtual
alone in these cases would yield true.
– Jeppe Stig Nielsen
Nov 27 at 14:29
add a comment |
up vote
28
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;
Your method also returns false for a method (or getter/setter of a property etc.) declaredsealed override
in C# (whether it implements an interface or not). Checking.IsVirtual
alone in these cases would yield true.
– Jeppe Stig Nielsen
Nov 27 at 14:29
add a comment |
up vote
28
down vote
accepted
up vote
28
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 Nov 27 at 7:00
Alexei Levenkov
83.4k889130
83.4k889130
answered Nov 27 at 4:10
vasily.sib
1,8871919
1,8871919
Your method also returns false for a method (or getter/setter of a property etc.) declaredsealed override
in C# (whether it implements an interface or not). Checking.IsVirtual
alone in these cases would yield true.
– Jeppe Stig Nielsen
Nov 27 at 14:29
add a comment |
Your method also returns false for a method (or getter/setter of a property etc.) declaredsealed override
in C# (whether it implements an interface or not). Checking.IsVirtual
alone in these cases would yield true.
– Jeppe Stig Nielsen
Nov 27 at 14:29
Your method also returns false for a method (or getter/setter of a property etc.) declared
sealed override
in C# (whether it implements an interface or not). Checking .IsVirtual
alone in these cases would yield true.– Jeppe Stig Nielsen
Nov 27 at 14:29
Your method also returns false for a method (or getter/setter of a property etc.) declared
sealed override
in C# (whether it implements an interface or not). Checking .IsVirtual
alone in these cases would yield true.– Jeppe Stig Nielsen
Nov 27 at 14:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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-does-a-property-inherited-from-an-interface-become-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