Skip to content

Instantly share code, notes, and snippets.

@r-moeritz
Created March 4, 2012 17:40
Show Gist options
  • Save r-moeritz/1974044 to your computer and use it in GitHub Desktop.
Save r-moeritz/1974044 to your computer and use it in GitHub Desktop.
ClojureCLR LoadEmbeddedAssembly
public static void load(String relativePath)
{
bool loadAsResource = EmbeddedAssembly(relativePath);
load(relativePath, true, loadAsResource);
}
private static bool EmbeddedAssembly(String relativePath)
{
return (ResourceData(ResourceName(relativePath)) != null);
}
private static string ResourceName(string relativePath)
{
return String.Concat(relativePath, ".clj")
.Replace('/', '_')
.Replace('.', '_');
}
private static byte[] ResourceData(string resourceName)
{
return (byte[]) Properties.Resources.ResourceManager.GetObject(resourceName);
}
public static void load(String relativePath, Boolean failIfNotFound, Boolean embeddedAssembly)
{
if (embeddedAssembly)
{
LoadEmbeddedAssembly(relativePath, failIfNotFound);
}
else
{
// Regular logic
}
}
private static void LoadEmbeddedAssembly(string relativePath, bool failIfNotFound)
{
string resourceName = ResourceName(relativePath);
byte[] assemblyData = ResourceData(resourceName);
if (assemblyData != null)
{
try
{
Var.pushThreadBindings(RT.map(CurrentNSVar, CurrentNSVar.deref(),
WarnOnReflectionVar, WarnOnReflectionVar.deref(),
RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
Compiler.LoadAssembly(assemblyData);
}
finally
{
Var.popThreadBindings();
}
}
else if (failIfNotFound)
throw new FileNotFoundException(String.Format("Could not locate embedded assembly {0}.", resourceName));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment