ILSpy "Failed to resolve assembly" in AstBuilder - c#

I want to decompile a method in my assembly, which references e.g. 'Microsoft.SharePoint.dll', but I have no SharePoint installed on the machine.
If I use the following code I get an Mono.Cecil.AssemblyResolutionException 'failed to resolve assembly Microsoft.SharePoint...'.
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("PathToMyAssembly");
ICSharpCode.Decompiler.DecompilerContext context = new ICSharpCode.Decompiler.DecompilerContext(assembly.MainModule);
AstBuilder decompiler = new AstBuilder(context);
decompiler.AddMethod(method); <!-- here it crashes -->
With the ILSpy GUI I can load my assembly without errors (on the same machine without SharePoint).
What do I need to change in my code?

I found the solution myself. I created my own AssemblyResolver which catches the AssemblyResolutionException and returns null for missing referenced assemblies.
public class MyDefaultAssemblyResolver : DefaultAssemblyResolver
{
public override AssemblyDefinition Resolve(AssemblyNameReference name)
{
try
{
return base.Resolve(name);
}
catch { }
return null;
}
public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
try
{
return base.Resolve(name, parameters);
}
catch { }
return null;
}
public override AssemblyDefinition Resolve(string fullName)
{
try
{
return base.Resolve(fullName);
}
catch { }
return null;
}
public override AssemblyDefinition Resolve(string fullName, ReaderParameters parameters)
{
try
{
return base.Resolve(fullName, parameters);
}
catch { }
return null;
}
}
Then I use it
var resolver = new MyDefaultAssemblyResolver();
resolver.AddSearchDirectory("FolderOfMyAssembly");
var parameters = new ReaderParameters
{
AssemblyResolver = resolver,
};
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("PathToMyAssembly", parameters);
I found the solution by debugging the ILSpy GUI (source code is fortunately available).

Related

Cannot GetTypes() in new domain

I have created a new domain, then loaded the assembly into this domain, but when GetTypes() gives an error like the picture attached, hope everyone helps, thanks.
Code
public class Program
{
public static void Main()
{
string assemblyPath = #"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug\BeyConsRevitProject.dll";
AppDomain appDomain = CreateChildDomain(AppDomain.CurrentDomain, Guid.NewGuid().ToString());
appDomain.AssemblyResolve += AssemblyResolve;
var value = (Proxy)appDomain.CreateInstanceAndUnwrap(typeof(Proxy).Assembly.FullName, typeof(Proxy).FullName);
var assembly = value.GetAssembly(assemblyPath);
var types = assembly.GetTypes();
Console.ReadKey();
}
private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName assemblyName = new AssemblyName(args.Name);
string dependentAssemblyFilename = Path.Combine(#"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug", assemblyName.Name + ".dll");
if (File.Exists(dependentAssemblyFilename)) return null;
return Assembly.LoadFile(dependentAssemblyFilename);
}
public static AppDomain CreateChildDomain(AppDomain parentDomain, string domainName)
{
Evidence evidence = new Evidence(parentDomain.Evidence);
AppDomainSetup setup = parentDomain.SetupInformation;
return AppDomain.CreateDomain(domainName, evidence, setup);
}
}
public class Proxy : MarshalByRefObject
{
public Assembly GetAssembly(string assemblyPath)
{
try
{
return Assembly.LoadFile(assemblyPath);
}
catch { return null; }
}
}
Error
Have you checked if BeyConsRevitProject.dll assembly is in the bin directory of your application? This is a possible cause. Try deleting the bin/ and obj/ folders and rebuilding your solution, if the error persists, use this code below to ascertain the real reason for the error:
using System.IO;
using System.Reflection;
using System.Text;
try
{
//The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if (exFileNotFound != null)
{
if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
//Display or log the error based on your application.
}
This code was suggested by Ben Gripka here:
Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

How to load assemblies in different folders C#

I need load a DLL and dependencies. In my database I already save all dependencies (path to the references files).
I.E:
DLL to load:
id: 1
name:"DummyModule.dll"
Dependencies:
DLL id: 1
path: "C:\DLL\ABC.dll"
AssemblyLoader class:
public class AssemblyLoader : MarshalByRefObject
{
public void Load(string path)
{
ValidatePath(path);
Assembly.Load(path);
}
public void LoadFrom(string path)
{
ValidatePath(path);
Assembly.LoadFrom(path);
}
public void LoadBytes(string path)
{
ValidatePath(path);
var b = File.ReadAllBytes(path);
Assembly.Load(b);
}
public Assembly GetAssembly(string assemblyPath)
{
try
{
return Assembly.Load(assemblyPath);
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
public Assembly GetAssemblyBytes(string assemblyPath)
{
try
{
var b = File.ReadAllBytes(assemblyPath);
return Assembly.Load(b);
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
private void ValidatePath(string path)
{
if (path == null)
throw new ArgumentNullException("path");
if (!File.Exists(path))
throw new ArgumentException(String.Format("path \"{0}\" does not exist", path));
}
}
The main class:
static void Main(string[] args)
{
string file1 = #"1\DummyModule.dll";
string file2 = #"2\PSLData.dll";
string file3 = #"3\Security.dll";
try
{
AppDomain myDomain = AppDomain.CreateDomain("MyDomain");
var assemblyLoader = (AssemblyLoader)myDomain.CreateInstanceAndUnwrap(typeof(AssemblyLoader).Assembly.FullName, typeof(AssemblyLoader).FullName);
assemblyLoader.LoadBytes(file2);
assemblyLoader.LoadBytes(file3);
var dummy = assemblyLoader.GetAssemblyBytes(file1);
foreach (var t in dummy.GetTypes())
{
var methodInfo = t.GetMethod("D");
if (methodInfo != null)
{
var obj = Activator.CreateInstance(t);
Console.Write(methodInfo.Invoke(obj, new object[] { }).ToString());
}
}
AppDomain.Unload(myDomain);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.ReadKey();
}
In the code above the "DummyModule.dll" is the main dll, "PSLData.dll" and "Security.dll" are the dependencies.
When I call the method "D" of my "DummyModule.dll" the error appears:
Could not load file or assembly 'DummyModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
All DLL files still in different folders. How I can load all needed files and call a function?
Thanks.
Try using this.. it worked for me..
serviceAgentAssembly =System.Reflection.Assembly.LoadFrom(string.Format(CultureInfo.InvariantCulture, #"{0}\{1}", assemblyPath, assemblyName));
foreach (Type objType in serviceAgentAssembly.GetTypes())
{
//further loops to get the method
//your code to ivoke the function
}
You're using relative paths to the assemblies, so the question is "relative to what?" The new AppDomain you've created and are loading assemblies into is lost in the woods; it doesn't inherit the same probe paths of the AppDomain in which you created it. Take a look at the class System.AppDomainSetup and its properties ApplicationBase and PrivateBinPath along with the form of CreateDomain() that takes an instance of AppDomainSetup as an argument. The simplest solution would be to use the AppDomainSetup instance returned by AppDomain.CurrentDomain.SetupInformation were the current domain is, of course, the app in which the new domain is created.

Load dll into another domain exception

Hi I am loading dll into another domain, it works fine when loaded into that domain but when i want some information from that domain through proxy object it gives me exception below is the code for review is there any wrong step ???
public class AssemblyProxy
{
System.Type[] _types;
public System.Type[] GetTypes()
{
return _types;
}
public string FullName { get; set; }
public void LoadAssembly(string path)
{
try
{
Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
AppDomain TestDomain = AppDomain.CreateDomain("AssemblyDomain", evidence, AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFullPath(path), true);
Proxy _asmProxy = (Proxy)TestDomain.CreateInstanceFromAndUnwrap(AppDomain.CurrentDomain.BaseDirectory+"Common.dll", typeof(Proxy).FullName);
_asmProxy.LoadAssembly(path);
FullName = _asmProxy.FullName;
_types = _asmProxy.GetTypes(); //Here i got Exception [Can not load file or assembly]
AppDomain.Unload(TestDomain);
}
catch (Exception ex)
{
}
}
}
class Proxy : MarshalByRefObject
{
System.Type[] _types;
public string FullName { get; set; }
public System.Type[] GetTypes()
{
return _types;
}
public void LoadAssembly(string path)
{
System.Reflection.Assembly _assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path));
_types = _assembly.GetTypes();
FullName = _assembly.FullName;
}
}
The exception I get is:
Can not load file or assembly
The way I solved this problem was by calling LoadFrom (not Load) and in the context of the AppDomain:
sDomain = AppDomain.CreateDomain(DOMAIN_NAME);
sDomain.DoCallBack(AppDomainCallback);
// runs in the context of the AppDomain
private void AppDomainCallback()
{
Assembly assembly = Assembly.LoadFrom(mAssemblyName);
}
I Have solved the issue by reading following Blog Post: the problem in my case is that i am returning System.Type Object from new domain which is no allowed you can return strings from proxy object but not System.Type object
Link

How to check if an assembly contains unit tests without loading it?

I'm currently using the following method to check for test assemblies:
private bool IsTestAssembly(string path)
{
var assembly = Assembly.LoadFrom(path);
foreach (var type in assembly.GetTypes())
{
var a = type.GetCustomAttributes(true).Select(x => x.ToString());
if (a.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute"))
return true;
}
return false;
}
But I would like to check this without loading the assembly in memory because I need to be able to delete it afterwards in case the verification fails.
I was hoping I could simply unload the assembly, but I soon discovered that, according to MSDN:
There is no way to unload an individual assembly without unloading all of the application domains that contain it.
Thanks in advance!
I worked out a short solution as suggested by TheGreatCO, i.e. to load the assembly in a new AppDomain:
1) Usage:
// assemblies are unloaded on disposal
using (var analyser = new AssemblyAnalyser())
{
var path = "my.unit.tests.dll";
var b = analyser.IsTestAssembly(path);
Assert.IsTrue(b);
}
2) Implementation:
public class AssemblyAnalyser : MarshalByRefObject, IDisposable
{
public AssemblyAnalyser()
{
var evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
var appSetup = new AppDomainSetup()
{
ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
};
appDomain = AppDomain.CreateDomain(otherDomainFriendlyName, evidence, appSetup);
}
public bool IsTestAssembly(string assemblyPath)
{
if (AppDomain.CurrentDomain.FriendlyName != otherDomainFriendlyName)
{
var analyser = appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, GetType().FullName);
return ((AssemblyAnalyser)analyser).IsTestAssembly(assemblyPath);
}
else
{
var assembly = Assembly.LoadFrom(assemblyPath);
return ContainsTestClasses(assembly);
}
}
public void Dispose()
{
if (AppDomain.CurrentDomain.FriendlyName != otherDomainFriendlyName)
{
AppDomain.Unload(appDomain);
GC.SuppressFinalize(this);
}
}
~AssemblyAnalyser()
{
Dispose();
}
private bool ContainsTestClasses(Assembly assembly)
{
foreach (var type in assembly.GetTypes())
{
var attr = type.GetCustomAttributes(true).Select(x => x.ToString());
if (attr.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute"))
return true;
}
return false;
}
private const string otherDomainFriendlyName = "AssemblyAnalyser";
private AppDomain appDomain;
}
Inspect the assemblies using Mono.Cecil. Cecil does not need to load the assembly to inspect it.

WPF - check resource exists without structured exception handling

Is there any way to check if a resource exists in an assembly without having to use exception handling? I'm currently loading images from several assemblies, and if they don't exist then I'm handling the IOException, which causes quite a bit of overhead.
Would something like this work for you?
// Member Variable
string [] resourceNames;
// Function
Boolean ResourceExists(string resourceName)
{
if (resourceNames == null)
{
resourceNames =
Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
return resourceNames.Contains(resourceName);
}
Copied answer from here:
public static bool ResourceExists(string resourcePath)
{
var assembly = Assembly.GetExecutingAssembly();
return ResourceExists(assembly, resourcePath);
}
public static bool ResourceExists(Assembly assembly, string resourcePath)
{
return GetResourcePaths(assembly)
.Contains(resourcePath);
}
public static IEnumerable<object> GetResourcePaths(Assembly assembly)
{
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
var resourceName = assembly.GetName().Name + ".g";
var resourceManager = new ResourceManager(resourceName, assembly);
try
{
var resourceSet = resourceManager.GetResourceSet(culture, true, true);
foreach(System.Collections.DictionaryEntry resource in resourceSet)
{
yield return resource.Key;
}
}
finally
{
resourceManager.ReleaseAllResources();
}
}

Categories

Resources