D
deja.com
My Goal:
I want to be able to use reflection to load an object from an assembly.
note: the object name is unknown and passed in from a config file, but
will have a known base class.
My Problem:
the following command sets t as 'null'; the type is not found.
Type t = Type.GetType("Joel.Net.SecurityHttpModule");
Workaround:
I've created some code to locate the assembly that contains the type...
string[] ignoreAsem = { "mscorlib", "System.Web", "System",
"System.Xml", "System.Data", "System.Web.Services", "System.Drawing",
"System.EnterpriseServices", "System.Web.Mobile",
"System.Web.RegularExpressions", "WebDev.WebHost", "msmetabase" };
foreach (Assembly asem in AppDomain.CurrentDomain.GetAssemblies()) {
if (Array.IndexOf(ignoreAsem, asem.GetName().Name) == -1) {
Response.Write(String.Format("{0}<BR>", asem.ToString()));
foreach (Type t in asem.GetTypes()) {
Response.Write(String.Format(" {0}<BR>", t.ToString()));
}
}
}
Once I find the name of the object I want to load, I can call
asem.GetType("Joel.Net.SecurityHttpModule") and it works.
Question:
Is this the right way of doing things? Is there an easier way?
Additional Comments:
If I compile the object and include it in the /bin directory, I know
the assembly name and can load it without any issues. The code example
here is attempting to retrieve an object from the /code directory. I
would like to be able to load either with the same code, in the
simplest method possible.
I want to be able to use reflection to load an object from an assembly.
note: the object name is unknown and passed in from a config file, but
will have a known base class.
My Problem:
the following command sets t as 'null'; the type is not found.
Type t = Type.GetType("Joel.Net.SecurityHttpModule");
Workaround:
I've created some code to locate the assembly that contains the type...
string[] ignoreAsem = { "mscorlib", "System.Web", "System",
"System.Xml", "System.Data", "System.Web.Services", "System.Drawing",
"System.EnterpriseServices", "System.Web.Mobile",
"System.Web.RegularExpressions", "WebDev.WebHost", "msmetabase" };
foreach (Assembly asem in AppDomain.CurrentDomain.GetAssemblies()) {
if (Array.IndexOf(ignoreAsem, asem.GetName().Name) == -1) {
Response.Write(String.Format("{0}<BR>", asem.ToString()));
foreach (Type t in asem.GetTypes()) {
Response.Write(String.Format(" {0}<BR>", t.ToString()));
}
}
}
Once I find the name of the object I want to load, I can call
asem.GetType("Joel.Net.SecurityHttpModule") and it works.
Question:
Is this the right way of doing things? Is there an easier way?
Additional Comments:
If I compile the object and include it in the /bin directory, I know
the assembly name and can load it without any issues. The code example
here is attempting to retrieve an object from the /code directory. I
would like to be able to load either with the same code, in the
simplest method possible.