|
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace AppConfigVerifier
{
public class AssemblyLoader : IDisposable
{
//class CrossDomainDataExtensions
//{
// public static Assembly GetAssembly(CrossDomainData data, AppDomain appDomain)
// {
// return appDomain.GetAssemblies().First((ass) => ass.FullName.Equals(AssemblyName.GetAssemblyName(data.Path).FullName));
// }
//}
class CrossDomainData : MarshalByRefObject
{
public bool HasType(string path, string typeName)
{
Assembly typeAssembly = Assembly.LoadFrom(path);
return typeAssembly.GetType(typeName, false, false) != null;
}
}
readonly AppDomain subDomain;
public AssemblyLoader()
{
var appDomainSetup = new AppDomainSetup
{
ApplicationBase = Path.GetDirectoryName(
GetType().Assembly.Location)
};
subDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, appDomainSetup);
AppDomain.CurrentDomain.AssemblyResolve += subDomain_AssemblyResolve;
}
Assembly subDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return Assembly.Load(args.Name);
}
#region IDisposable Members
volatile bool disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~AssemblyLoader()
{
Dispose(false);
}
void Dispose(bool disposing)
{
if (!disposed)
{
disposed = true;
if (!disposing)
{
}
AppDomain.Unload(subDomain);
AppDomain.CurrentDomain.AssemblyResolve -= subDomain_AssemblyResolve;
}
}
#endregion
public bool HasType(string assemblyPath, string typeName)
{
if (disposed)
{
throw new InvalidOperationException("AssemblyLoader already disposed");
}
if (typeName == null)
{
throw new ArgumentNullException("typeName");
}
var crossData = (CrossDomainData)subDomain.CreateInstanceAndUnwrap(
GetType().Assembly.FullName, typeof(CrossDomainData).FullName);
return crossData.HasType(assemblyPath, typeName);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts
I am currently the CTO of
Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.
I also developed a tool to make IaaS on Azure more easy to use
IaaS Management Studio.
If you want to contact me, go
this way