-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPlugin.cs
84 lines (75 loc) · 3.4 KB
/
MyPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using XrmToolBox.Extensibility;
using XrmToolBox.Extensibility.Interfaces;
namespace AshV.PortalTranslator.XTB
{
// Do not forget to update version number and author (company attribute) in AssemblyInfo.cs class
// To generate Base64 string for Images below, you can use https://www.base64-image.de/
[Export(typeof(IXrmToolBoxPlugin)),
ExportMetadata("Name", "ML"),
ExportMetadata("Description", "Portal ML"),
// Please specify the base64 content of a 32x32 pixels image
ExportMetadata("SmallImageBase64", null),
// Please specify the base64 content of a 80x80 pixels image
ExportMetadata("BigImageBase64", null),
ExportMetadata("BackgroundColor", "Lavender"),
ExportMetadata("PrimaryFontColor", "Black"),
ExportMetadata("SecondaryFontColor", "Gray")]
public class MyPlugin : PluginBase
{
public override IXrmToolBoxPluginControl GetControl()
{
return new MyPluginControl();
}
/// <summary>
/// Constructor
/// </summary>
public MyPlugin()
{
// If you have external assemblies that you need to load, uncomment the following to
// hook into the event that will fire when an Assembly fails to resolve
// AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolveEventHandler);
}
/// <summary>
/// Event fired by CLR when an assembly reference fails to load
/// Assumes that related assemblies will be loaded from a subfolder named the same as the Plugin
/// For example, a folder named Sample.XrmToolBox.MyPlugin
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
private Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
{
Assembly loadAssembly = null;
Assembly currAssembly = Assembly.GetExecutingAssembly();
// base name of the assembly that failed to resolve
var argName = args.Name.Substring(0, args.Name.IndexOf(","));
// check to see if the failing assembly is one that we reference.
List<AssemblyName> refAssemblies = currAssembly.GetReferencedAssemblies().ToList();
var refAssembly = refAssemblies.Where(a => a.Name == argName).FirstOrDefault();
// if the current unresolved assembly is referenced by our plugin, attempt to load
if (refAssembly != null)
{
// load from the path to this plugin assembly, not host executable
string dir = Path.GetDirectoryName(currAssembly.Location).ToLower();
string folder = Path.GetFileNameWithoutExtension(currAssembly.Location);
dir = Path.Combine(dir, folder);
var assmbPath = Path.Combine(dir, $"{argName}.dll");
if (File.Exists(assmbPath))
{
loadAssembly = Assembly.LoadFrom(assmbPath);
}
else
{
throw new FileNotFoundException($"Unable to locate dependency: {assmbPath}");
}
}
return loadAssembly;
}
}
}