|
-
June 23rd, 2006, 09:01 AM
#1
Is there a way to instantiate by name?
I'd like to avoid switch/case statements and/or keeping track of some sort of indexing array/collection.
I need to be able to do something like:
Code:
public Iclasstype makeObject(string makeme)
{
Iclasstype myobjectvar = new ("classprefix" + makeme)
// OR
Iclasstype myobjectvar = new System.SomeByNameFunction("classprefix" + makeme)
}
-
June 23rd, 2006, 10:29 AM
#2
Re: Is there a way to instantiate by name?
Have a look at the System.Reflection namespace.
Useful or not? Rate my posting. Thanks.
-
June 23rd, 2006, 11:44 AM
#3
Re: Is there a way to instantiate by name?
Code:
Type theType = Type.GetType(typename);
IClassType ict = (IClassType)Activator.CreateInstance(theType);
if the assembly containing that type has not been loaded into the app domain, it wont be able to find it using Type.GetType(...), instead you'll need to load the assembly containing that class, then use the assembly object to GetType & pass in a fully qualified type string.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|