How to properly get Type by ReflectionOnly for methods usage

I need to use .dll libraries located in specified file within new domain by reflection only. Everything is just fine, I can get information, methods, types, but when I actually want to Invoke one of methods, exception is raised System.Reflection.TargetException (Object does not match target object). Glitch is, when I set as "path" only name of the library "Filter.dll" and have library in Debug directory, app works. When I move library and change its path to @"D:/Dropbox/SchoolProject/MyPlugins/Filter.dll", exception is raised.


My code:



AppDomain domain = AppDomain.CreateDomain("MyNewDomain"); //new domain for assembly
//parameters are @"D:/Dropbox/SchoolProject/MyPlugins/Filter.dll", "Plugins.Filter"
ObjectHandle objh = domain.CreateInstanceFrom(_selectedLibraryDirectives.Item1 + _selectedLibraryDirectives.Item2 + ".dll", _selectedLibraryDirectives.Item3);
object obj = objh.Unwrap();

if (RemotingServices.IsTransparentProxy(obj))
{
Type domainType = null;
foreach (var ass in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies())
{
//domainType's name and fullname is "Filter", "Plugins.Filter"
//so I would think this is type I want, but exception about wrong
//type is raised when using domainType for method.Invoke
domainType = ass.DefinedTypes.First().AsType();
}

//type's name and fullname is "Filter", "Plugins.Filter",
//when Filter.dll is in Debug dir, even if name of the file in
//domain.CreateInstanceFrom is set to path to MyPlugins, invoking method works
//and "MarshalByRefObject", "System.MarshalByRefObject" in case library
// isn't in Debug directory, invoking method crashes on wrong type
Type type = obj.GetType();
//get method by method name previously added as ListBox Item
MethodInfo method = domainType.GetMethod(PluginMethodsListBox.SelectedItem.ToString());
//exception is usually raised with invoking
Tuple<string, string> tuple = (Tuple<string, string>)method.Invoke(domainType, new object[] { "hello" });
PluginsPluginNameValueLabel.Text = method.Name;
PluginsPluginDescValueLabel.Text = tuple.Item2;
}


This is first time I am dealing with libraries, reflection only and such. I am confused, why I can use library in one folder, but not in other folder, even if conditions are the same. And when I can get MethodInfo from library in other than default folder, why I cannot get its Type properly.


Rest of code for getting information about methods in libraries and saving it to UI elements, when library is selected by user:



foreach (var library in _libraries)
{
if (PluginsListBox.SelectedItem + ".dll" == library.Name)
{
//getting library info
Assembly ass = Assembly.ReflectionOnlyLoadFrom(library.DirectoryName + @"\" + library.Name);
var types = ass.GetTypes();
Type type = null;
foreach (var t in types)
{
if (t.Name + ".dll" == library.Name)
{
_selectedLibraryDirectives = new Tuple<string, string, string>(library.DirectoryName +@"\", t.Name, t.FullName);
type = t;
}
}
//viewing list of methods in ListBox
if (type != null)
{
PluginMethodsListBox.Items.Clear();
foreach (MethodInfo method in type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
{
if (method.ReturnType == typeof(Tuple<string,string>) && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType.FullName == "System.String")
{
var a = method.GetParameters();
PluginMethodsListBox.Items.Add(method.Name);
}

}
}
}
}


Thanks in regards for any advice for solving or understanding this situation.


Edit (library code):



[Serializable]
public class Filter : MarshalByRefObject
{
public void noParamConsTest() { Console.WriteLine("noparamconsoletest");}
public void paramConstTest(string s) { Console.WriteLine(s); }
public Tuple<string,string> FilterCommas(string text)
{
//..
return new Tuple<string, string>(returnString, string.Empty);
}


Solution:


I didn't have enought time for propper, clean and elegant solution, so I changed approach a bit. I set file watcher over plugins dir and when change occured, I just copied library to CurrentDomain working dir. Load assembly from there is no problem at all.


But my question stand unanswered. Why AppDomains, even if they have set base path, relative path etc to custom path, look for libraries in default working dir instead of specified or in both of them. Load libraries only from custom dir to new domain seems to be impossible to me.


If someone knows explanation, let me know please. Thank you