What is "__nonvirtual"? And why does this decompiled C# code look weird?
I have a DLL open in dot peek and it's showing some code that looks weird to me.
On one line I have this:
__nonvirtual (((UIElement) this).OnPreviewMouseRightButtonUp(e));
I have an idea of what this line does, but I don't see how this is valid C#. What is that "__nonvirtual"?
On another line, I have this:
public MyClass()
{
base..ctor();
}
Again, it's clear to me what this code does, but this is not valid C#. That's not how you call a base constructor from a derived class.
Am I misunderstanding something with regards to using dot peek? On your website here http://confluence.jetbrains.com/display/NETCOM/Introducing+JetBrains+dotPeek it says that "dotPeek decompiles any .NET assemblies and presents them as C# code". Unless I'm really wrong, this code is not valid C# code. It does look like C# code though. What gives?
Please sign in to leave a comment.
Hello? Is anybody here? I still don't have an answer to this question and it's been a week. Is this forum abandoned?
Hello,
Sorry for a delay. Did you have a 'Show Compile-Generated Code' option enabled?
If you have this option disabled, could you please provide us this dll for investigation? It would be highly appreciated.
Thank you.
I can't send you the DLL because I work in a corporate environment, but I can send you screenshots. I've attached two screenshots, one showing what the output looks like when I show compiler-generated code (Show.PNG) and one showing what it looks like when I hide compiler-generated code (Hide.PNG). I've circled the interesting parts in red. The base..ctor() remains in both (with some kind of encoding artifact) and the __nonvirtual remains in both. So that setting is not the problem.
Attachment(s):
Hide.PNG
Show.PNG
Got same unicode siquences when tried to "Go to Source" in VS 2010.
The class in question was not really existing in C# but was a COM Interop.
The
__nonvirtual
keyword you're seeing in the decompiled code is not a standard C# keyword. It's likely an artifact of the decompilation process².In C#, methods are non-virtual by default, meaning they cannot be overridden in a derived class³. When you see
__nonvirtual
in decompiled code, it's likely indicating a call to a non-virtual method².In your specific example,
__nonvirtual (((UIElement) this).OnPreviewMouseRightButtonUp(e))
is simply calling theOnPreviewMouseRightButtonUp
method ofthis
, which is cast toUIElement
. In standard C#, you would see this written as((UIElement)this).OnPreviewMouseRightButtonUp(e)
.⁶Decompiled code can sometimes include artifacts or non-standard syntax due to differences between the original source code and the compiled code. Decompilers do their best to produce readable and accurate C# code, but the result might not always match the original source code².
Sources:
(1) What is "__nonvirtual"? And why does this decompiled C# ... - JetBrains. https://dotnettools-support.jetbrains.com/hc/en-us/community/posts/205832699-What-is-nonvirtual-And-why-does-this-decompiled-C-code-look-weird-.
(2) virtual - C# Reference - C# | Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/virtual.
(3) Calling Virtual and Non-virtual Methods for C# | Pluralsight. https://www.pluralsight.com/guides/calling-virtual-and-non-virtual-methods-csharp.
(4) Why doesn't C# have a non-virtual calling convention?. https://stackoverflow.com/questions/3378570/why-doesnt-c-sharp-have-a-non-virtual-calling-convention.
(5) NETCOM. http://confluence.jetbrains.com/display/NETCOM/Introducing+JetBrains+dotPeek.
(6) UIElement.OnPreviewMouseRightButtonUp(MouseButtonEventArgs) Method .... https://learn.microsoft.com/en-us/dotnet/api/system.windows.uielement.onpreviewmouserightbuttonup?view=windowsdesktop-7.0.