This question already has an answer here:
Despite the many examples I still do not get it.
- Type.GetType("namespace.a.b.ClassName") returns null
- Resolve Type from Class Name in a Different Assembly
- C# creating instance of class and set properties by name in string
- Activator.CreateInstance: Could not load type from assembly
- Could not load type from assembly error
- Get a new object instance from a Type
- Object reference not set to an instance of an object. C# GetProperty()
I got 1 visualstudio solution with multiple projects inside
1 of those projects is a console application
namespace Test
--> assembly name is Test
1 of those projects is a class library
namespace Mayhem
--> assembly name is Mayhem
The class library has multiple classes
namespace Mayhem
{
public class Step1
{
public int Id { get; set; }
public string Title { get; set; }
}
}
I can access the class library from the console application. But I would like to access the class library through Reflection. Based on some other solutions I came up with something like this, but it still gives me a TypeLoadException
namespace Test
{
class Program
{
private static void Main(string[] args)
{
string[] ClassArray = new string[] { "Mayhem.Step1", "Mayhem.Step2" };
foreach (var item in ClassArray)
{
Type type = Type.GetType(item, true);
object instance = Activator.CreateInstance(type);
PropertyInfo prop = type.GetProperty("Title");
prop.SetValue(instance, item, null);
Console.WriteLine(SetSegment.ToMessageSegment(instance));
}
}
}
}
Question: what am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire