This question already has answers here:
Which version of C# am I using
(20 answers)
Closed 3 years ago.
I use C# for .NET. I know how to programmatically detect CLR version of a .NET assembly, but can’t find out how to programmatically detect the C# version in my code. Anyone has idea? TIA.
The C# source code has been translated to IL when the assembly is generated. It's no longer relevant what C# version was used to generate the IL (or indeed, if C# was used), nor is that information stored.
There are some IL features that will not be used by older versions of C# (e.g. support for nullable reference types and async streams). If the IL contains such constructs, you can rule out older C# versions. It's highly non-trivial to make that determination.
Here's a breakdown of the assembly format (click through to the second article in the series). You'll see there's no information describing the language or version that generated the assembly.
https://www.red-gate.com/simple-talk/blogs/anatomy-of-a-net-assembly-pe-headers/
Related
This question already has answers here:
CLR and CLI - What is the difference?
(7 answers)
Closed 5 years ago.
What exactly is Common Language Runtime in Dot Net. Is it an exe or dll. Can I see common language runtime in task manager. Where is it located.
You should first look up in google before posting here.. But here it goes:
What is it?
The .NET Framework provides a run-time environment called the common language runtime, which runs the code and provides services that make the development process easier.
Where is it located?.
C:\Windows\Microsoft.NET\Framework\v2.0.50727
Of course the last part depends on your version.
This question already has answers here:
What is the language of compilers? Are they written with different languages?
(5 answers)
In what language is C# compiled [closed]
(3 answers)
What are modern and old compilers written in?
(7 answers)
Closed 5 years ago.
Looking at the github repo, I see that roslyn is written in C# and VB, the same languages that it is meant to compile. It would make sense that each version is compiled using the version that came before it but, if that is the case, how was the first ever version compiled?
Its called bootstrapping
I can tell thats confusing, it's a kind of "the chicken and egg" problem, but it has a rather elegant solution.
In basic terms what you do is make a basic version of the compiler in a pre-existing language compiler.
Using that compiler you compile a compiler code for the language written in itself.
Imagine you write a c# compiler in java, and then you using that compiler written in java to compile and run a compiler written in c# that knows how to compile c#.
But then you say "But what about the first compiler ever, there was no language before that!" its answered here, in short: they wrote a compiler in literal bytes just like a compiler would emit for a cpu to run. after that worked they could theoretically write a compiler in that language.
This question already has answers here:
Call C# dll from Delphi
(3 answers)
Closed 7 years ago.
As my first time as a student employee programming I have to create a DLL in C#.NET which is callable from Delphi.
I've read about this being not so easy due to Delphi being native code and .NET being managed.
I've also read about 'Unmanaged Exports' from Robert Giesecke. Could this be the solution?
Since I do not have Delphi on my machine (Delphi isn't free), I can't test this out for myself.
Also, will I have problems with my datatypes when I implement the Unmanaged Exports?
Last but not least, is there a language I could use that would have the same problem as Delphi for loading the DLL? It'd be nice for me to be able to test.
UnmanagedExports is one option. It certainly works very well. Other options include:
Exposing the functionality using COM.
Exposing the functionality via a C++/CLI mixed mode assembly.
Hosting the CLR from your Delphi code.
The final option is not very tractable. Don't attempt to do that.
You'll almost certainly encounter problems with interop data types unless you are already an expert in the field.
You can certainly test this out with FreePascal. Very likely you can write code in FreePascal that consumes your .net assembly and later use the exact same code in Delphi. Even if you have to change it somewhat, the lessons you will learn will be exactly the same.
You can make your .Net DLL as Com visible. Check this link.
Delphi can load Com objects without problems. This in theory, I don't have specific code samples, but you can try with different tutorials, they practically explains the same.
I don't know about the data types, but COM always tries to cast to a base type (double, float to decimal, number to integer, and complex objects to dynamic objects).
You can use the .Net Runtime Library for Delphi in http://dotnetruntimelibraryfordelphi.sourceforge.net/
The runtime library allows you to host and access .net library types.
This question already has answers here:
C# library to native C++ application
(3 answers)
Closed 3 years ago.
What is the best solution to access c# code via native code (C++)?
I have C# code which I want to call from native project, so I'm considering writing a COM wrapper but I wonder whether there is a better option (framework/design pattern/architecture etc.) available in .NET?
COM interop is without a doubt the best bet. It's a known, supported framework for achieving exactly what you want to achieve.
There is another alternative however - it is possible to edit the IL code inside a compiled .NET assembly to flag methods within the assembly as native exports. A detailed breakdown of the changes can be found in this CodeProject article.
Robert Giesecke has created Unmanaged Exports to simplify this process using simple method attributes. A NuGet package can be found here.
This question already has answers here:
Is is possible to export functions from a C# DLL like in VS C++?
(4 answers)
Closed 6 years ago.
I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.
I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.
How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?
Thanks,
Andrew
Have a look at Unmanaged Exports.
Write a .Net library in Managed C++ and there you can export a "Native" method/function.
Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods.
It is possible as one guy has shown a reasonable solution
on codeproject or here,
but it requires a post build step and you are on your own after that.
I don't know if this hack will work on .net 4.0 or later though.
Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.
What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.