Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Created November 19, 2014 14:10
Show Gist options
  • Save lennybacon/87f8f2a3f894347b1a80 to your computer and use it in GitHub Desktop.
Save lennybacon/87f8f2a3f894347b1a80 to your computer and use it in GitHub Desktop.
Loading Licenses into the LicenseContext
public class LicenseLoader
{
public static void LoadLicensesFromCallingAssembly()
{
var assembly = Assembly.GetCallingAssembly();
foreach (var resourceName in
assembly.GetManifestResourceNames().
Where(resourceName => resourceName.EndsWith(".licenses")))
{
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
Deserialize(
stream,
resourceName.Substring(0, resourceName.Length - 9).
ToUpper(CultureInfo.InvariantCulture),
LicenseManager.CurrentContext
);
}
}
}
internal static void Deserialize(
Stream o,
string cryptoKey,
LicenseContext context)
{
var obj = new BinaryFormatter().Deserialize(o);
if (!(obj is object[]))
{
return;
}
var objArray = (object[])obj;
if (!(objArray[0] is string) || (string) objArray[0] != cryptoKey)
{
return;
}
var type = context.GetType();
var field =
type.GetField(
"savedLicenseKeys",
BindingFlags.NonPublic | BindingFlags.Instance
);
if (field != null)
{
field.SetValue(context, objArray[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment