Reputation: 4383
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".
Upvotes: 3
Views: 1169
Reputation: 245028
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)
{ }
}
Upvotes: 6