c# - Weird behavior using Activator.CreateInstance -
Say that I have defined the following class hierarchy in the class library:
Public Interface IFoo {String GetMsg (); } Public abstract class FooBase {public virtual string GetMsg () {return "Foobase msg"; }} Public Square Foo: Phoebe, IFu {#region IFoo member public new string GetMsg () {return.GetMsg (); } #endregion}
I am consuming this assembly in a console application and using the reflection is making an example of the Foo class typed in IFoo as follows: / P>
assembly A = assembly. Loadfile (pathgatefulpath ("TestClassLib.dll")); Var typeDiff = a.GetType ("TestClassLib.Foo"); Var fooInst = Activator.CreateInstance (TypeDef) as IFoo; String msg = fooInst.GetMsg ();
The above work is fine now if I get this code and keep it on an ASP.NET web web page:
Namespace Test webpace {public Partial Category _Default: System.Web.UI.Page {Secure Zero Page_load (Object Sender, EventArgs E) {string filePath = Server.MapPath (@ "~ / bin / TestClassLib.dll"); Assembly A = assembly Loadfile (pathgatefulpath (filepath)); Var typeDiff = a.GetType ("TestClassLib.Foo"); Var fooInst = Activator.CreateInstance (TypeDef) as IFoo; String msg = fooInst.GetMsg (); }}}
fooInst is on the following line:
var fooInst = Activator.CreateInstance (typeDef) as IFoo;
When I debug a web page, what's interesting is the definition of a valid type in the 'typeDef' variable and what's odd if I type Activator.CreateInstance (typeDef ) The result results in the Watch Studio as IFoo in the Watch Window is not futile!
What am I missing here?
PS - I have already verified that the assembly (TestClassLib.dll) exists in the ASP.NET App's bin directory.
My guess is that you are withdrawing the object from the octirator. CreateInstance, but the defendant artist ("In the form of IFoo") is failing, possibly due to the load context (see, because you have loaded the assembly from LoadFrom - see). Is the IFoo perchance defined in both calling assemblies and which you are loading dynamically? It may be that fusion try to load the assembly by typing a Type.GetType () with the name of an assembly-worthy type so that you get the same results (also to make sure that "CreateInstance is giving you " some back).
Comments
Post a Comment