Create class instance in assembly from string name
- by Arcadian
I'm not sure if this is possible, and I'm quite new to using assemblies in C#.NET.
What I would like to do is to create an instance of a class when supplied the string name of that class. Something like this:
using MyAssembly;
namespace MyNameSpace
{
Class MyClass
{
int MyValue1;
int MyValue2;
public MyClass(string myTypeName)
{
foreach(Type type in MyAssembly)
{
if((string)type == myTypeName)
{
//create a new instance of the type
}
}
AssignInitialValues(//the type created above)
}
//Here I use an abstract type which the type above inherits from
private void AssignInitialValues(AbstractType myClass)
{
this.value1 = myClass.value1;
this.value2 = myClass.value2;
}
}
}
Obviously you cannot compare strings to types but it illustrates what I'm trying to do: create a type from a supplied string.
Any thoughts?
EDIT:
After attempting:
var myObject = (AbstractType) Activator.CreateInstance(null, myTypeName);
AssignInitialValues(myObject);
I get a number of errors:
Inconsistent accessibility: parameter type 'MyAssembly.AbstractType' is less accessible than method 'MyNameSpace.MyClass.AssignInitialValues(MyAssembly.AstractType)'
'MyAssembly.AstractType' is inaccessible due to it's protection level
The type or namespace name 'MyAssembly' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AbstractType' could not be found (are you missing a using directive or an assembly reference?)
Not exactly sure why it can't find the assembly; I've added a reference to the assembly and I use a Using Directive for the namespace in the assembly. As for the protection level, it's calling classes (or rather the constructors of classes) which can only be public.
Any clues on where the problem is?
UPDATE:
After looking through several articles on SO I came across this: http://stackoverflow.com/a/1632609/360627
Making the AbstractTypeclass public solved the issue of inconsistent accessibility.
The new compiler error is this:
Cannot convert type 'System.Runtime.Remoting.ObjectHandle' to 'MyAssembly.AbstractType'
The line it references is this one:
var myObject = (AbstractType) Activator.CreateInstance(null, myTypeName);
Using .Unwrap() get's me past this error and I think it's the right way to do it (uncertain). However, when running the program I then get a TypeLoadException when this code is called.
TypeLoadException: Could not load type ‘AbstractType’ from assembly ‘MyNameSpace'...
Right away I can spot that the type its looking for is correct but the assembly it's looking in is wrong. Looking up the Activator.CreateInstance(String, String) method revealed that the null as the first argument means that the method will look in the executing assembly. This is contrary to the required behavior as in the original post.
I've tried using MyAssembly as the first argument but this produces the error:
'MyAssembly' is a 'namespace' but is used like a 'variable'
Any thoughts on how to fix this?