问题
I know .NET and Mono are binary compatible but given a set of source code, will csc and mcs produce the exact same 100% identical binary CLI executable? Would one be able to tell whether an executable was compiled with csc or mcs?
回答1:
A lot of things are not fully defined in the spec, or are implementation-specific extensions.
Examples of not-fully-specified:
- the synchronisation semantics of field-like events; this is explicitly open to implementations in the ecma spec; it is stricty defined in the MS spec, but uses a different version in c# 4.0 which does not yet appear in the formal spec, IIRC
Expression
construction (from lambdas); is simply "defined elsewhere" (actually, it isn't)
Examples of implementation extensions:
- P/Invoke
- COM interface handling (i.e. how you can call
new
on an interface)
So no: it is not guarantees to have the same IL, either between csc or [g]mcs - but even between different versions of csc.
Even more: depending on debug settings, optimizations being enabled or not, and some compilation constants being defined (such as DEBUG or TRACE), the same compiler will generate different code.
回答2:
I'm sure they don't produce the same IL from the same sourcecode. Even different versions of the MS C# compiler don't.
The optimizer will work differently and create slightly different code.
I expect larger differences in the implementation of complex features such as iterators, local variable captured for lamdas,...
Then there are arbitrary compiler generated names, for example for anonymous types. No reason why they should use the same naming scheme for them.
I wouldn't be surprised if there was some assembly metadata containing the name and version of your compiler too.
来源:https://stackoverflow.com/questions/5850142/compatibility-of-net-csc-and-mono-mcs