, , Framework, Framework..NET , IPlugin . , ( , InvalidCastException, null, ).
A
, - , - , , , Framework. , .
, , - :
public class PluginWrapper : IPlugin
{
object fObj;
PropertyInfo fNameProperty;
MethodInfo fGetOtherMethod;
public PluginWrapper(object o)
{
fObj = o;
fNameProperty = o.GetType().GetInterface("IPlugin").GetProperty("Name");
fGetOtherMethod = o.GetType().GetInterface("IPlugin").GetMethod("GetOther", new Type[] { typeof(string) });
}
public string Name
{
get { return (string)fNameProperty.GetValue(fObj, null); }
}
public IOther GetOther(string name)
{
object result = fGetOtherMethod.Invoke(fObj, new object[] { name });
if (result == null)
return null;
return new OtherWrapper(result);
}
}
:
IPlugin iPlugIn = new PluginWrapper(o);
B
. , , , "" , .
AssemblyName. , Framework AssemblyResolve:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly dll = Assembly.Load(File.ReadAllBytes(@"C:\Program Files\WinApp\Assembly.dll"));
object o = Activator.CreateInstance(dll.GetType("WinApp.ClassThatImplementsIPlugIn"));
IPlugin iPlugIn = o as IPlugin;
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.ToLower().StartsWith("framework,"))
return typeof(IPlugin ).Assembly;
return null;
}
, .NET , . - , .
, bindingredirect app.config. , , .