Skip to content

Instantly share code, notes, and snippets.

View kbrammer's full-sized avatar

Kevin Brammer kbrammer

View GitHub Profile
@kbrammer
kbrammer / DataMemberAttribute Name Extension Method
Created March 18, 2013 18:00
Return DataMemberAttribute names for a DataContract object or property
//Returns DataMemberAttribute names for a DataContract object
public static List<string> GetDataMemberAttributeNames(object obj)
{
List<string> result = new List<string>();
foreach (PropertyInfo p in obj.GetType().GetProperties())
{
foreach (System.Runtime.Serialization.DataMemberAttribute ca in p.GetCustomAttributes(false))
{
result.Add((string)ca.Name);
}
@kbrammer
kbrammer / IEnumerableToHTMLTable.cs
Last active December 15, 2015 02:59
Extension method to automatically create a generic HTML table from an IEnumerable<object>
public static object TableBuilder(IEnumerable<object> obj)
{
XElement result = new XElement("table");
//add header row using property names
result.Add( new XElement("tr",
obj.FirstOrDefault().GetType().GetProperties().Select (property => new XElement("th",property.Name))));
//add object values to table rows
foreach(object o in obj)
@kbrammer
kbrammer / gist:5235255
Created March 25, 2013 06:26
Array.toDataTable() - Extends the array object to return a Google Visualization DataTable object. Depends on LINQjs.
Array.prototype.toDataTable = function () {
var results = {};
results.cols = Enumerable.From(this[0]).Select(function (p) {
return { id: p.Key, label: p.Key, type: "string" };
}).ToArray();
results.rows = Enumerable.From(this).Select(function (c) {
return { c: Enumerable.From(results.cols).Select(function (i) {
return { v: c[i.id] };
}).ToArray()
@kbrammer
kbrammer / PowerPointConversionAutomation.cs
Last active December 16, 2015 07:29
LINQPad script to migrate PowerPoint files from .ppt to .pptx using Microsoft.Office.Interop.Powerpoint
//using office 14.0.0.0
//using Microsoft.Office.Interop.Powerpoint
//http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint._application(v=office.14).aspx
public static Microsoft.Office.Interop.PowerPoint.Application ppt { get; set; }
void Main()
{
ppt = new Microsoft.Office.Interop.PowerPoint.Application();
MigratePPT(@"C:\Users\MyFiles");
@kbrammer
kbrammer / GitHubEnvironment.ps1
Last active December 16, 2015 17:59
Snippet to call the environment setup script to run GitHub Shell from Visual Studio Express 2012 Package Manager console
. (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
@kbrammer
kbrammer / LINQPadSearchFileContents.cs
Created May 3, 2013 18:09
Extension methods to enumerate files and directories, include or exclude by name or extension, and search file contents. Example is in LINQPad format.
void Main()
{
string path = Path.GetFullPath(@"C:\Users\Username");
System.IO.DirectoryInfo di = new DirectoryInfo(path);
var query = di.GetFiles("*.*",SearchOption.TopDirectoryOnly).IncludeFileExtensions("*.asp").SearchFileContents("searchterm");
query.Dump();
}
public static class FileIOExtensions
@kbrammer
kbrammer / LINQPadGitHubAPI.cs
Created May 3, 2013 18:18
Simple example of calling GitHub API from LINQPad and serializing the returned JSON
void Main()
{
System.Net.WebClient client = new System.Net.WebClient();
JavaScriptSerializer js = new JavaScriptSerializer();
var url = "https://api.github.com/repos/kbrammer/kevinbrammer.azurewebsites.net/contents";
var results = client.DownloadString(url);
Content[] r = js.Deserialize<Content[]>(results);
r.Dump("Results");
}
@kbrammer
kbrammer / ExportQueryObjs.vb
Last active August 29, 2015 13:56
Export SQL Query Objects from Microsoft Access 2013
' http://msdn.microsoft.com/en-us/library/office/gg278834(v=office.15).aspx
Sub ExportQueryObjs()
' File System Properties
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
' DAO / Query definition properties
' http://stackoverflow.com/questions/20252599/how-do-you-export-ms-access-query-objects-to-text-file
Dim def As DAO.QueryDef
@kbrammer
kbrammer / https-service.js
Last active February 26, 2022 14:17
node https get and post request as promise with optional JWT
const https = require("https");
// based on https://stackoverflow.com/questions/35182752/promises-with-http-get-node-js
module.exports = {
get(urlString, token = null) {
return new Promise((resolve, reject) => {
const url = new URL(urlString);
const requestOptions = {
method: "GET",
hostname: url.hostname,