Hi,
I have an .aspx page that needs to instantiate an object whose type in
not known until runtime. The definition of said object resides in
/App_code/CMS.cs
CMS.cs
==================
namespace CMS{
public interface IWindmill{
string emit
{get;}
}
public class MyObject : IWindmill{
string html;
public MyObject(){
html = "Hello World"
}
public string emit{
get { return html; }
}
}
In my webpage, I am trying to instantiate myObject dynamically, like
so:
Default.aspx.cs
============
protected void Page_Load(object sender, EventArgs e){
Type t = Type.GetType("CMS.MyObject"); //This object will vary and
won't be determined until runtime
CMS.IWindmill myObj = (CMS.IWindmill)Activator.CreateInstance(t);
MethodInfo m = myObj.GetType().GetMethod("get_emit");
if (m != null){
string s = (string)m.Invoke(myObj, null);
Response.Write(s);
}
}
My Type t will return null unless I include the CMS namespace in the
Default.aspx.cs file. Is there a way for me to return the proper value
if the object is defined in an external file? I understand I could
package the CMS namespace in an assembly and reference it through its
qualified name, but is there an easier and more direct way to get the
type in an external .cs file?
Regards,
Chris