Skip to content

Instantly share code, notes, and snippets.

View DinkDev's full-sized avatar

Dale Thompson DinkDev

View GitHub Profile
@DinkDev
DinkDev / DynamicXml.cs
Created July 16, 2019 19:35
Converting an XDocument to ExpandoObject for dyanmic element access.
namespace DynamicLinqToXml
{
using System.Dynamic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
[TestClass]
@DinkDev
DinkDev / LockedFileHelper.cs
Created November 8, 2019 18:10
Utility class to check if a file is locked.
public static class LockedFileHelper
{
public static bool IsFileLocked(string fileName)
{
return IsFileLocked(new FileInfo(fileName));
}
public static bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
@DinkDev
DinkDev / Exceptions.Data access
Created November 14, 2019 16:34
A gist of an extension method to help simplify accessing the Exception's Data property.
/// <summary>
/// Exception Helper extension methods for using Data dictionary.
/// </summary>
public static class ExceptionExtensions
{
public static bool HasData(this Exception ex, Func<object, bool> keyFilter)
{
keyFilter = keyFilter ?? (o => true);
return ex.Data.Cast<DictionaryEntry>().Any(datum => keyFilter(datum.Key));
@DinkDev
DinkDev / AppBootstrapper.cs
Created November 18, 2019 15:50
Autofac wire up for Caliburn.Micro Boostrapper.
namespace Vastec.Ocr.Evaluator.Wpf
{
using System;
using System.Collections.Generic;
using Autofac;
using Caliburn.Micro;
using Properties;
using ViewModels;
/// <remarks>
@DinkDev
DinkDev / Untitled-2
Created January 10, 2020 20:54
CodingHorror-AFewTooManyDependencies.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Atalasoft.Annotate;
using Atalasoft.Imaging.WinControls;
@DinkDev
DinkDev / AppBootstrapper-Caliburn-Autofac-Serilog-and-dotnet-Core-Loggning.cs
Last active January 27, 2020 18:25
Caliburn with Autofac, Serilog and using .NET Core style ILogger.
namespace Vastec.Ocr.Evaluator.Wpf
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Autofac;
using Caliburn.Micro;
using Serilog;
using AzureImpl.Parsers;
@DinkDev
DinkDev / ObfuscationByConvolution_2019_01_16.cs
Last active January 16, 2020 13:15
CodingHorror-So many bad practices here: private calling public, exception catching method tailored to a single method, swallowed errors without any logging, poor use of type checking, multiple returns, etc. This could be a simnple method passing a lambda to another. (Gotta love that hidden delegate method call too).
public class derived
{
#region dateTimePicker1_ValueChanged Event
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
ControlBase.Arg1ExceptionLogging(DtpValueChangedAction, sender, "dateTimePicker1_ValueChanged", "Date Picker Button");
}
public void DtpValueChangedAction(object sender)
@DinkDev
DinkDev / BadToBetterCode_2020_02_04.cs
Created February 4, 2020 14:25
An example of some poor code that could easily be done better.
namespace MessyCode
{
/// <summary>
/// The following issues were created in the BadCode() example:
/// • Casting fsResultG.BoxFolderId which is a Nullable<long> (or better, long?) to long is a problem. Be sure to use the .Value property!
/// • The only thing in the first call that needs to be cast, is the result of the result of Deserialize(), everything else is properly typed.
/// • There is a possibility that fsResultG or fsResultG.BoxFolderId is null, this must be checked before the call since only a long value is accepted – this is a hidden crash error!
/// • Please use the null propagating operators (?? And ?.) It’s silly to write code like:
/// o finalizeData2 == null || finalizeData2.FolderFormList == null || finalizeData2.FolderFormList.Count < 1
/// o ReSharper will fix this for you in most cases.
@DinkDev
DinkDev / t.txt
Created February 20, 2020 14:13
Pluralsight Core Python test file - so it won't be blocked.
It was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
it was the epoch of belief
it was the epoch of incredulity
it was the season of Light
it was the season of Darkness
it was the spring of hope
it was the winter of despair
@DinkDev
DinkDev / AutoMapperLearningTests.cs
Created February 20, 2020 19:44
AutoMapper Dictionary to property bag mapping, with some ExpandoObject utilities.
namespace LearningMoreAutoMapper
{
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AutoMapperLearningTests