jeudi 14 juin 2018

Accessing a class in other namespace but same solution [duplicate]

This question already has an answer here:

Despite the many examples I still do not get it.

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