Skip to content

Instantly share code, notes, and snippets.

View nguerrera's full-sized avatar
🌴
I no longer work at Microsoft

Nick Guerrera nguerrera

🌴
I no longer work at Microsoft
View GitHub Profile
@nguerrera
nguerrera / DelegateToStaticWithFirstArg.cs
Last active August 29, 2015 14:05
Creating a delegate bound to a static method with first argument in verifiable IL
// And this program can be writen in C# after all if you use an extension method!
// (And the codegen is exactly as the optimal hand-written IL below.)
//
// Thanks to Vladimir Sadov on the compiler team for teaching me about it!
static class Program
{
static void Main()
{
var f = new Func<string>("World".StaticMethod);
@nguerrera
nguerrera / ConfusableIdentifiers.cs
Last active August 29, 2015 14:05
This is valid and prints False then 42. (Hint: Unicode)
using System;
static class Program
{
static bool A()
{
return false;
}
static int А()

@davkean's thoughts on "contract breaking" exceptions:

You do not want to derive from InvalidOperationException. It and ArgumentException (and derivatives) are considered "contract breaking" exceptions.

Contract breaking exceptions should only be thrown to indicate that the caller has a bug. I pretend that every time I go to throw these exceptions that the process will be torn down immediately. If I’m okay with that, then I throw it, if not, then I should be throwing a

@nguerrera
nguerrera / UnsafeWithoutKeyword.cs
Last active October 15, 2019 13:38
Absence of 'unsafe' C# keyword/switch does not guarantee type or memory safety.
/*
The absence of the C# unsafe keyword and switch does not guarantee code
is type or memory safe. It merely prevents the use of pointer types and
fixed size buffers.
The only thing that can guarantee type and memory safety is
verification and CAS/transparency enforcement.
In particular, the absence of unsafe C# blocks does NOT:
II.23.2.12 Type
Type ::=
BOOLEAN | CHAR | I1 | U1 | I2 | U2 | I4 | U4 | I8 | U8 | R4 | R8 | I | U
| ARRAY ModifiableType ArrayShape (general array, see §II.23.2.13)
| CLASS TypeDefOrRefOrSpecEncoded
| FNPTR MethodDefSig
| FNPTR MethodRefSig
| GENERICINST (CLASS | VALUETYPE) TypeDefOrRefOrSpecEncoded GenArgCount ModifiableType*
| MVAR number
@nguerrera
nguerrera / GetMethodBodyViaSRM.cs
Created December 5, 2015 18:59
GetMethodBodyViaSRM
/*
"dependencies": {
"System.Console": "4.0.0-beta-23516",
"System.IO.FileSystem": "4.0.1-beta-23516",
"System.Reflection": "4.1.0-beta-23516",
"System.Reflection.Metadata": "1.1.0",
"System.Reflection.TypeExtensions": "4.1.0-beta-23516"
}
*/
@nguerrera
nguerrera / PartialPropertyOverride.cs
Created March 11, 2016 22:28
Partial property override
class X { }
class Foo {
public virtual X Y { get; set; }
public virtual X Z { get; set; }
}
class Bar : Foo {
public override X Y { set { } }
public override X Z { get { return null; } }
@nguerrera
nguerrera / EmbedProposal.md
Last active July 25, 2016 22:36
Archive of PDB embedding proposal

This proposal addresses #5397, which requests a feature for embedding source code inside of a PDB.

I am committed to implementing this with whatever changes fall out from the review if it is approved. I have an initial implementation in a WIP PR (#12353) that I will evolve based on feedback here. Some details here are new based on recent offline feedback and not yet matched by the implementation.

Scenarios

Recap from #5397

  • During the build, source code is auto-generated and then compiled. This auto-generated source does not exist on source control server and is often not preserved as a build artifact. Even if it is preserved, it can't be indexed on a symbol server making acquisition difficult at debug time.
  • A company is OK from an IP standpoint to release source for some of their projects, but their source control system is behind a firewall. Their IT security policies prevent giving any external access to the source control system, which prevents typical usage of source server. They already
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="DesktopOnlyDependency">
<Version>1.0.0</Version>
</PackageReference>
</ItemGroup>
static void OpenEmbeddedResources(string path)
{
using (var peStream = File.OpenRead(path))
using (var peReader = new PEReader(peStream))
{
var mdReader = peReader.GetMetadataReader();
foreach (var resourceHandle in mdReader.ManifestResources)
{
var resource = mdReader.GetManifestResource(resourceHandle);