Skip to content

Instantly share code, notes, and snippets.

View danielmackay's full-sized avatar

Daniel Mackay [SSW] danielmackay

View GitHub Profile
@danielmackay
danielmackay / Property Display Name.cs
Created September 4, 2014 05:00
Looks up the DisplayName attribute on the property defined by the expression. If the type of the expression is generic (e.g. IEnumerable of T) we will look up the inner type instead. #reflection, #expression, #razor
public static class PropertyHelper
{
/// <summary>
/// Looks up the DisplayName attribute on the property defined by the expression. If the type
/// of the expression is generic (e.g. IEnumerable of T) we will look up the inner type instead.
/// </summary>
public static string GetDisplayName<TModel, T>(Expression<Func<TModel, T>> property)
{
var fieldName = ExpressionHelper.GetExpressionText(property);
@danielmackay
danielmackay / IEnumerable ForEach Extension.cs
Created August 21, 2014 04:01
IEnumerable ForEach extension.
public static partial class EnumerableExt
{
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
}
}
}
@danielmackay
danielmackay / ControllerExt.cs
Created August 20, 2014 02:15
MVC View Model and Model Validation. #FluentValidation
public static class ControllerExt
{
public static bool TryValidateAndTranslate(this Controller controller, object model, string prefix, object propertyMap)
{
return TryValidateAndTranslate(controller, model, prefix, new RouteValueDictionary(propertyMap));
}
public static bool TryValidateAndTranslate(this Controller controller, object model, string prefix, RouteValueDictionary propertyMap)
{
ModelMetadata metadata = ModelMetadataProviders
@danielmackay
danielmackay / Data Table Convertor.cs
Created May 20, 2014 01:36
Converts any IList<T> into a data table. #.net, #datatable
public static class Conversion
{
public static DataTable ToDataTable<T>(this IList<T> list , string tableName)
{
var dataTable = new DataTable(tableName);
var properties = TypeDescriptor.GetProperties(typeof(T))
.Cast<PropertyDescriptor>()
.Where(p => p.PropertyType.Namespace.Equals("System"))
.ToArray();
var values = new object[properties.Length];
@danielmackay
danielmackay / Animated Expander.css
Created April 17, 2014 01:20
Animated expander using ng-show. #angularjs
.expander {
-webkit-transition: all cubic-bezier(0.250, 0.460, 0.450, 0.940) .5s;
-moz-transition: all cubic-bezier(0.250, 0.460, 0.450, 0.940) .5s;
-o-transition: all cubic-bezier(0.250, 0.460, 0.450, 0.940) .5s;
transition: all cubic-bezier(0.250, 0.460, 0.450, 0.940) .5s;
height: 100px;
}
.expander.ng-hide {
height: 0px;
@danielmackay
danielmackay / Azure Trace Apender.cs
Created April 7, 2014 08:55
Azure Log4net appender. This will produce output in the streaming log service in visual studio. #log4net, #azure.
public class AzureTraceAppender : TraceAppender
{
protected override void Append(LoggingEvent loggingEvent)
{
var level = loggingEvent.Level;
var message = RenderLoggingEvent(loggingEvent);
if (level >= Level.Error)
Trace.TraceError(message);
else if (level >= Level.Warn)
Trace.TraceWarning(message);
@danielmackay
danielmackay / Array.Contains.js
Created March 21, 2014 00:40
Javascript function for checking if a simple value exists in an array. #javascript, #array
if (Array.prototype.contains){
throw "Array.contains already exists";
}
Array.prototype.contains = function(item) {
for (var i = 0; i < this.length; i++) {
if (this[i] === item)
return true;
}
@danielmackay
danielmackay / property sorting.js
Created March 20, 2014 08:04
Sorting the properties in a javascript object. #javascript, #sorting
var obj = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
var sortable = [];
@danielmackay
danielmackay / LINQpad with xml.cs
Created March 20, 2014 03:42
Query XML with LINQ from within LINQpad.
void Main()
{
var xml = XElement.Load (@"C:\\Temp\\ModelWheelSize.xml");
var parent = xml.Descendants("ModelWheelSize")
.Where(e => e.Element("ModelID").Value == "747")
.Dump();
}
@danielmackay
danielmackay / Array.removeByKey.js
Last active August 29, 2015 13:57
Javascript function for removing an item from an array by a key. #javascript, #array.
Array.prototype.removeByKey = function (key, value) {
var i = this.length;
while(i--){
if (this[i][key] === value) {
return this.splice(i, 1);
}
}
return null;
};