Skip to content

Instantly share code, notes, and snippets.

View Warrenn's full-sized avatar

Warrenn Enslin Warrenn

  • Busyweb
  • South Africa
View GitHub Profile
@Warrenn
Warrenn / displayContext
Created April 24, 2015 13:57
Display Context
public static class HtmlHelperExtension
{
public static string ActiveCss<T>(this HtmlHelper helper, T pageId, string activeClass = "active")
{
var displayContext = helper.ViewData.DisplayContext<T>();
if (EqualityComparer<T>.Default.Equals(displayContext, pageId))
{
return activeClass;
}
return string.Empty;
@Warrenn
Warrenn / viewdata extension
Created April 24, 2015 13:58
ViewData Extension
public static class ViewDataExtension
{
public static void DisplayContext<T>(this ViewDataDictionary viewData, T instance, string key = null)
{
var keyValue = (string.IsNullOrEmpty(key)) ? typeof(T).FullName : typeof(T).FullName + "." + key;
viewData[keyValue] = instance;
}
public static T DisplayContext<T>(this ViewDataDictionary viewData, string key = null)
{
@Warrenn
Warrenn / DisposableChannelFactory
Last active August 29, 2015 14:19
Disposable Channel Factory
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.ServiceModel;
namespace DisposableChannelFactory
{
public class ClientGenerator
@Warrenn
Warrenn / GenericExpando
Created July 1, 2015 14:43
A dynamic object that can be used to call Methods when only Type object is available
using System;
using System.Collections.Concurrent;
using System.Dynamic;
using System.Linq;
using System.Reflection;
public class GenericExpando : DynamicObject
{
private readonly object instance;
private readonly Type staticType;
@Warrenn
Warrenn / TryParser
Last active June 16, 2017 14:42
Safely convert any primative struct or DateTime without needing to know the type using that type's TryParse Method
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class TryParser
{
private delegate bool TryParseDelegate<T>(string stringValue, out T instance) where T : struct;
@Warrenn
Warrenn / ExceptionHandler
Created July 1, 2015 14:50
A useful Method to wrap the Enterprise Library ExceptionHandler safely calls finalize and ensures that logging will happen
using System;
using LoadFileData.Constants;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
public static class ExceptionHandler
{
public static void Dispose(this IDisposable disposable, string policyName = PolicyName.Disposable)
{
if (disposable == null)
{
@Warrenn
Warrenn / AssemblyHelper
Created July 1, 2015 14:58
Load any Type from the assembly(ies) without the ReflectionTypeLoadException and include Generics
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public static class AssemblyHelper
{
public static IEnumerable<Type> LoadableTypesOf(Type loadableType)
{
@Warrenn
Warrenn / Create-Xdt.ps1
Last active June 22, 2022 06:37
Powershell script to create a XML-Document-Transform. This auto generates a transform file by comparing a production file to the development file and figuring out what the transform needs to look like in order to transform the development file to the production equivalent
function Create-Xdt(
[string] $prodConfig,
[string] $devConfig,
[string] $xdtFile,
[string[]] $keepNodes = @(),
[hashtable] $findReplace = @{},
[string[]] $keyAttributes = @("name", "key", "path", "contract")
)
{
if([System.String]::IsNullOrEmpty($prodConfig) -or
@Warrenn
Warrenn / transform.ps1
Last active August 23, 2021 14:51
Powershell script to transform a config file using a XML-Document-Transform file
function XmlDocTransform($xml, $xdt, $output)
{
if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
throw "File not found. $xml";
}
if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
throw "File not found. $xdt";
}
$transformDll = ""
@Warrenn
Warrenn / Invoke-ElevatedCommand.ps1
Created September 23, 2015 14:52 — forked from TaoK/Invoke-ElevatedCommand.ps1
Invoke-ElevatedCommand for Administrator-level commands in a common powershell pipeline
Function Invoke-ElevatedCommand {
<#
.DESCRIPTION
Invokes the provided script block in a new elevated (Administrator) powershell process,
while retaining access to the pipeline (pipe in and out). Please note, "Write-Host" output
will be LOST - only the object pipeline and errors are handled. In general, prefer
"Write-Output" over "Write-Host" unless UI output is the only possible use of the information.
Also see Community Extensions "Invoke-Elevated"/"su"
.EXAMPLE