vendredi 3 août 2018

getting Types of referenced project via Reflection

I'm getting the Type of a class via given [string] name of that class.

// a local (in this startup project) public class works fine
Type test = System.Reflection.TypeInfo.GetType("Learning.Models.Production");
Console.WriteLine(test.Name.ToString()); 
// prints "Production"

Now, in my solution I have 3 different (referenced) projects. In each of these I can get the Types of it's own Project classes. But I cannot get them cross-project wise:

// a public class from a referenced project in this solution - returns null
Type test2 = System.Reflection.TypeInfo.GetType("JeffData.Store");
Console.WriteLine(test2.Name.ToString()); 
// System.NullReferenceException (because test2 is null)

further testing:

// this is working
Type t1 = System.Reflection.TypeInfo.GetType("System.String");
Console.WriteLine(t1.Name.ToString()); // prints "String"

// but not this (also a public class)
Type t2 = System.Reflection.TypeInfo.GetType("System.Web.HtmlString");
Console.WriteLine(t2.Name.ToString());

// neither that: a public class of another referenced assembly - returns null
Type t3 = System.Reflection.TypeInfo.GetType("Newtonsoft.Json.JsonConvert");
Console.WriteLine(t3.Name.ToString());

From other somehow related questions (though very old) I tested to set
[assembly: ComVisible(true)] without success (I also learned that I should rather not do that unless really needed).

I also tried setting Embed Interop Type in the reference properties to true, because it sounded like it could help:

enter image description here

So the question is what else I can do to be able to get the Type of such a referenced project.





Aucun commentaire:

Enregistrer un commentaire