I'm trying to port an existing C# class (a generic factory) that uses reflection, but I can't get this piece of code to compile:
Type[] types = Assembly.GetAssembly(typeof(TProduct)).GetTypes();
foreach (Type type in types)
{
if (!typeof(TProduct).IsAssignableFrom(type) || type == typeof(TProduct))
...
I tried looking at the Reflection in the .NET Framework for Windows Metro Style Apps and Assembly Class, where I found an example that didn't compile because of the "using System.Security.Permissions".
Just like the first page you linked to says, you need to use TypeInfo instead of Type. There are also other changes, for example, Assembly has a DefinedTypes property instead of GetTypes() method. The modified code could look like this:
var tProductType = typeof(TProduct).GetTypeInfo();
var types = tProductType.Assembly.DefinedTypes; // or .ExportedTypes
foreach (var type in types)
{
if (!tProductType.IsAssignableFrom(type) || type == tProductType)
{ }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With