0

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?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Bunkerbuster
  • 963
  • 9
  • 17
  • 2
    Have you tried the full name: "Mayhem.Step1, AssemblyName" where `AssemblyName` would be the actual name of the assembly? – Wiktor Zychla Jun 14 '18 at 13:59
  • 1
    From the accepted answer on your very first link - "Type.GetType("namespace.qualified.TypeName") only works when the type is found in either mscorlib.dll or the currently executing assembly.". Do you believe that either of those conditions are true for your type? And a quick check of the docs confirms that `GetType(string,boolean)` has the same restriction. – Damien_The_Unbeliever Jun 14 '18 at 14:02
  • @wiktor which assembly? This string[] ClassArray = new string[] { "Mayhem.Step1,Mayhem", "Mayhem.Step2,Mayhem" }; this is sadly not working. – Bunkerbuster Jun 14 '18 at 14:41
  • @Damien Type.GetType("namespace.qualified.TypeName") ==> I dit not fully understand the answer (as I already stated) Type.GetType("Mayhem.?.Step1"); What do I fill in the Answer? – Bunkerbuster Jun 14 '18 at 14:45
  • `Mayhem.Step1, Mayhem` should **definitely** work, unless you miss the reference between assemblies or the class is not public. – Wiktor Zychla Jun 15 '18 at 07:08

0 Answers0