Skip to content

Instantly share code, notes, and snippets.

View szul's full-sized avatar
💻

Michael Szul szul

💻
View GitHub Profile
@szul
szul / DynamicMethodInvocation.cs
Last active August 29, 2015 14:20
Dynamic method invocation
/*
* You will need "using System.Reflection;"
* The variable "method" is a string that represents the case-sensitive name of the method to be called.
* The variable "context" in this instance is the HttpContext, but can be any variable(s) the method expects.
* In this example the called method returns a string, so the invocation is cast into a string, but this can change based on what the method returns.
*/
Type _t = this.GetType();
MethodInfo _methodInfo = _t.GetMethod(method);
string result = (String)_methodInfo.Invoke(this, new object[] { context });
@szul
szul / DynamicFactoryLoading.cs
Last active August 29, 2015 14:20
Dynamic factory loading
/*
* You will need "using System.Reflection;"
* This method allows you to dynamically load a specific factory that inherits from a base factory.
* The variable "factoryAssembly" is a string that represents the path to the factory DLL.
* The variable "factoryClass" is a string that represents class to be instantiated.
*/
public BaseFactory GetFactory(string factoryAssembly, string factoryClass)
{
Assembly assembly = Assembly.LoadFile(factoryAssembly);
@szul
szul / TurnOffCodeFirst.cs
Created June 16, 2015 14:02
Turning off code first migrations in ASP.NET MVC and Web API for when legacy databases are used
protected void Application_Start()
{
//Where DatabaseContext is the class name of your database context for MVC/Web API
Database.SetInitializer<DatabaseContext>(null);
}
@szul
szul / HttpContextConversion.cs
Created June 29, 2015 12:40
HttpContext to HttpContextBase and vice versa
//HttpContextBase to HttpContext (assumes HttpContextBase variable named contextBase)
HttpContext context = contextBase.ApplicationInstance.Context;
//HttpContext to HttpContextBase (assumes HttpContext variable named context)
HttpContextBase contextBase = new System.Web.HttpContextWrapper(context);
@szul
szul / RecursiveCTE.sql
Last active September 17, 2015 00:25
Using a CTE for a recursive select
WITH CTE_ITEM_HIERARCHY AS
(
SELECT i.[ItemID]
,i.[Text]
,i.[ParentItemID]
FROM [dbo].[Item] AS i
WHERE i.[ParentItemID] = 0
UNION ALL
@szul
szul / sftp.ps1
Created January 21, 2016 20:03
PowerShell Script for use with Team Foundation Server as a build step to transfer files. Assumes pscp.exe from the puTTY team.
param(
[string]$files,
[string]$username,
[string]$password,
[string]$url
)
try {
Start-Process 'C:\puTTY\pscp.exe' -ArgumentList('-sftp -v -r -hostkey "TARGET_SERVER_HOST_KEY" -l ' + $username + ' -pw "' + $password + '" ' + $files + ' ' + $url) -Wait
}
@szul
szul / nuget-install-example.md
Last active May 14, 2016 15:06
Example README for updating NuGet when there are issues with ASP.NET MVC and ASP.NET Web API 2

Update NuGet

If your references are still causing you issues when you attempt to build, you can try the following commands from the NuGet Package Manager Console:

Update-Package -ProjectName '<PROJECT_NAME>' -Reinstall

Install-Package Microsoft.AspNet.WebApi.HelpPage

Update-Package -ProjectName '<PROJECT_NAME.TEST>' -Reinstall

//Assumes you've created a nuspec file using nuget spec FILE_NAME.csproj
<Target Name="AfterBuild">
<Exec Command="nuget pack FILE_NAME.csproj -IncludeReferencedProjects" />
</Target>
@szul
szul / project.json
Created July 3, 2016 14:46 — forked from darrelmiller/project.json
Project.json comparison with XML equivalent
{
"authors": [
"Sam Saffron",
"Marc Gravell",
"Nick Craver"
],
"owners": [
"marc.gravell",
"nick.craver"
],
@szul
szul / EventCalendar.cs
Created February 20, 2018 16:26
Code snippet for ICS attachment creation
public sealed class EventCalendar
{
private string _ics;
public EventCalendar()
{
}
public EventCalendar(string summary, string description, System.DateTime start, System.DateTime end, string location)
{
StringBuilder sb;