Created
July 9, 2015 14:48
-
-
Save julesx/41bc9002cffb0370090d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainWindow : Window | |
{ | |
private Session _session; | |
private Project _project; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
Closing += MainWindow_Closing; | |
} | |
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) | |
{ | |
_session.Dispose(); | |
} | |
private void Button_Start(object sender, RoutedEventArgs e) | |
{ | |
if (_session == null) | |
{ | |
var sessionMgr = SessionManager.Create("MyHostApp"); | |
_session = sessionMgr.CreateSession(); | |
} | |
_session.Ide.Show(); | |
} | |
private void Button_New(object sender, RoutedEventArgs e) | |
{ | |
if (_session == null || _project != null) | |
{ | |
//Error handling... | |
return; | |
} | |
var storage = ProjectStorage.CreateStorageFromTemplate("template.vstax", | |
"csaddin", | |
"VSTASample", | |
null); | |
_project = _session.LoadProject(storage); | |
} | |
private void Button_Save(object sender, RoutedEventArgs e) | |
{ | |
if (_project != null) | |
{ | |
MyProjectStorage outputProject = new MyProjectStorage("ProjectName"); | |
_project.Save(outputProject); | |
} | |
} | |
private void Button_Run(object sender, RoutedEventArgs e) | |
{ | |
var binaryManager = _project.BinaryManager; | |
// Find the BinaryItem for the output assembly and its .pdb file. | |
string assemblyName = binaryManager.AssemblyName; | |
string pdbName = Path.GetFileNameWithoutExtension(assemblyName) + ".pdb"; | |
BinaryItem assemblyItem = null; | |
BinaryItem pdbItem = null; | |
foreach (BinaryItem item in binaryManager.GetBinaryItems()) | |
{ | |
string itemName = item.Name; | |
if (String.Equals(itemName, assemblyName, StringComparison.OrdinalIgnoreCase)) | |
{ | |
assemblyItem = item; | |
if (pdbItem != null) | |
break; | |
} | |
else if (String.Equals(itemName, pdbName, StringComparison.OrdinalIgnoreCase)) | |
{ | |
pdbItem = item; | |
if (assemblyItem != null) | |
break; | |
} | |
} | |
if (assemblyItem == null) | |
{ | |
//Error handling... | |
return; | |
} | |
// Get the assembly as a byte[]. | |
Stream assemblyStream = assemblyItem.GetStream(); | |
byte[] assemblyBytes = new byte[assemblyStream.Length]; | |
assemblyStream.Read(assemblyBytes, 0, assemblyBytes.Length); | |
// Get the .pdb as a byte[]. | |
Stream pdbStream = pdbItem.GetStream(); | |
byte[] pdbBytes = new byte[pdbStream.Length]; | |
pdbStream.Read(pdbBytes, 0, pdbBytes.Length); | |
// Load the raw assembly. | |
Assembly assembly = Assembly.Load(assemblyBytes, pdbBytes); | |
// Find the AddIn class. | |
Type addInType = assembly.GetTypes().FirstOrDefault((t) => t.Name == "AddIn"); | |
if (addInType == null) | |
{ | |
//Error handling... | |
return; | |
} | |
// Find the Init method. | |
MethodInfo initMethod = addInType.GetMethod("Init"); | |
if (initMethod == null) | |
{ | |
//Error handling... | |
return; | |
} | |
// Find my custom method. this only works because i regenerated manifest.xml | |
MethodInfo customMethod = addInType.GetMethod("Custom"); | |
if (customMethod == null) | |
{ | |
//Error handling... | |
return; | |
} | |
// Initialize the add-in | |
initMethod.Invoke(null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment