Skip to content

Instantly share code, notes, and snippets.

View davkean's full-sized avatar

David Kean davkean

View GitHub Profile
@davkean
davkean / gist:986d8ac6cd65f585e442
Created August 20, 2014 16:59
Leaking enumerable
DirectoryInfo info = new DirectoryInfo(@"C:\Temp");
IEnumerable<FileInfo> files = info.EnumerateFiles("*.txt")
.Where(f => f.CreationTimeUtc > lastWeek);
foreach (FileInfo file in files)
{
} // With your proposal, where does Dispose get called on the strong-typed enumerable returend by EnumerateFiles?
@davkean
davkean / gist:13c551718b0b6d6850de
Created August 22, 2014 06:06
"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 different exception type.
Examples of contract breaks:
- You passed me index that was greater or equal to the number elements in the collection (returned by Count property)
- You passed me null, when I need a non-null value
- You passed me an empty string, when I need a non-empty string
- You called Start, but you’ve not set the ExecutablePath property.
=== Logging started: 1/7/2015 15:53:20 ===
Action 15:53:20: ADMIN.
Action start 15:53:20: ADMIN.
Action 15:53:20: CostInitialize. Computing space requirements
Action start 15:53:20: CostInitialize.
Action ended 15:53:20: CostInitialize. Return value 1.
Action 15:53:20: FileCost. Computing space requirements
Action start 15:53:20: FileCost.
Action ended 15:53:20: FileCost. Return value 1.
Action 15:53:20: CostFinalize. Computing space requirements
Getting the following building with Visual Studio and Windows 10?
The "GenerateResource" task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly 'file:///C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Build.Tasks.v12.0\v4.0_12.0.0.0__b03f5f7f11d50a3a\Microsoft.Build.Tasks.v12.0.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
File name: 'file:///C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Build.Tasks.v12.0\v4.0_12.0.0.0__b03f5f7f11d50a3a\Microsoft.Build.Tasks.v12.0.dll' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569
@davkean
davkean / AssemblyLoadFile.md
Last active June 2, 2017 22:36
Why Assembly.LoadFile is not the right API to call

Assembly.LoadFile is almost always the wrong API to call inside an application. It says to the CLR “hands off, this assembly will live in its own little universe and don’t you apply any of your normal rules for loading assemblies”. Below you’ve effectively loaded two of these universes and one doesn’t see the other. LoadFile also forces you to hook onto AssemblyResolve (like you’ve done below[1]) and tell the CLR about every dependency that assembly needs to resolve. It also opts you out of other things such as binding redirects, retargeting (v1 portable won’t load below) and a few other things.

Assembly.LoadFile is used by designers, and should be called Assembly.LoadFile_ForDesignerUseOnly, it’s unfortunate it has such as tempting name.

If this is inside of VS, it is most definitely not the correct API to call. You want to use Assembly.Load, Assembly.LoadFrom and/or use VS related APIs for registering binding paths. It already has an assembly resolve, and no one else should be hooking onto it.

[1] That

This file has been truncated, but you can view the full file.
Build started 11/19/2015 4:56:27 PM.
1>Project "d:\j\workspace\roslyn_prtest_win_dbg_unit32\BuildAndTest.proj" on node 1 (BuildAndTest target(s)).
1>Project "d:\j\workspace\roslyn_prtest_win_dbg_unit32\BuildAndTest.proj" (1) is building "d:\j\workspace\roslyn_prtest_win_dbg_unit32\Roslyn.sln" (2) on node 1 (Build target(s)).
2>ValidateSolutionConfiguration:
Building solution configuration "Debug|Mixed Platforms".
The target "AfterGenerateAppxManifest" listed in an AfterTargets attribute at "C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\Microsoft.Net.CoreRuntime.targets (68,11)" does not exist in the project, and will be ignored.
The target "_GeneratePrisForPortableLibraries" listed in a BeforeTargets attribute at "C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\Microsoft.Net.CoreRuntime.targets (177,11)" does not exist in the project, and will be ignored.
The target "AfterGenerateAppxManifest" listed in an AfterTargets attribute at "C:\Program Files (x86)
@davkean
davkean / project.xml
Created February 26, 2016 16:14
Converting Content -> AndroidAssert
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Import Project="..\SharedProject\SharedProject.projitems" Label="Shared" />
<ItemGroup>
<AndroidAsset Include="@(Content)" />
</ItemGroup>
...
</Project>
This file has been truncated, but you can view the full file.
Build started 8/25/2016 4:06:23 PM.
Environment at start of build:
ALLUSERSPROFILE = C:\ProgramData
APPDATA = C:\Users\davkean\AppData\Roaming
BatchFile = build
BinariesDirectory = D:\roslyn-project-system\bin\Debug\
BuildConfiguration = Debug
CommandPromptType = Native
CommonProgramFiles = C:\Program Files (x86)\Common Files
CommonProgramFiles(x86) = C:\Program Files (x86)\Common Files
This file has been truncated, but you can view the full file.
Build started 1/9/2017 4:32:10 PM.
Environment at start of build:
ALLUSERSPROFILE = C:\ProgramData
APPDATA = C:\Users\davkean\AppData\Roaming
BatchFile = build
BinariesDirectory = E:\project-system2\bin\Debug\
BuildConfiguration = Debug
CommonProgramFiles = C:\Program Files (x86)\Common Files
CommonProgramFiles(x86) = C:\Program Files (x86)\Common Files
CommonProgramW6432 = C:\Program Files\Common Files
Build started 1/9/2017 5:09:52 PM.
Environment at start of build:
ALLUSERSPROFILE = C:\ProgramData
APPDATA = C:\Users\davkean\AppData\Roaming
BatchFile = build
BinariesDirectory = E:\project-system2\bin\Debug\
BuildConfiguration = Debug
CommonProgramFiles = C:\Program Files (x86)\Common Files
CommonProgramFiles(x86) = C:\Program Files (x86)\Common Files
CommonProgramW6432 = C:\Program Files\Common Files