Skip to content

Instantly share code, notes, and snippets.

@eirikb
Created April 24, 2013 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirikb/5455792 to your computer and use it in GitHub Desktop.
Save eirikb/5455792 to your computer and use it in GitHub Desktop.
Automatically re-load DLL file and invoke external method with SPWeb as parameter. Re-run on DLL re-build
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.SharePoint;
namespace SharePointHack
{
public class Program : MarshalByRefObject
{
public SPWeb Web { get; set; }
public string FilePath { get; set; }
public void Run()
{
var appDomain = AppDomain.CreateDomain("SharePointDomain");
appDomain.DoCallBack(() =>
{
var assembly = Assembly.Load(File.ReadAllBytes(FilePath));
assembly.GetTypes().ToList().ForEach(type => type.GetMethods().ToList().ForEach(method =>
{
if (method.Name != "Main") return;
method.Invoke(null, method.GetParameters().Length == 0 ? null : new object[] {Web});
}));
});
AppDomain.Unload(appDomain);
}
public static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Please enter file (DLL) and SPWeb url");
return;
}
var file = args[0];
var dir = Path.GetDirectoryName(file);
var w = new FileSystemWatcher(dir);
var timer = DateTime.Now;
using (var site = new SPSite(args[1]))
{
using (var web = site.OpenWeb())
{
w.Changed += (sender, args2) =>
{
if ((DateTime.Now - timer).Seconds < 2) return;
var p = new Program {Web = web, FilePath = file};
p.Run();
timer = DateTime.Now;
};
}
}
w.EnableRaisingEvents = true;
Console.WriteLine("Ready");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment