Why do I get an "Unrecognized method, property, or field 'IsInterface'" error when accessing NET.assembly properties with .NET 9

With .NET 9, I am attempting to get the properties of a NET.Assembly (like Classes or Interfaces), but this throws an error. For example, 
>> asmInfo = NET.addAssembly("System.Windows.Forms");
>> asmInfo.Classes
Unrecognized method, property, or field 'IsInterface' for class 'System.RuntimeType'.
...
This same code works with .NET 8. Why am I getting an error with .NET 9?

 Accepted Answer

There are two possible workarounds for this issue:
1) Use a different version of .NET.
2) If you need .NET 9, you can use reflection to inspect the types inside of an assembly. For example, you can use the code in the following example to display the Classes in an assembly.
asmInfo = NET.addAssembly("System.Collections");
h = asmInfo.AssemblyHandle;
types = h.GetTypes();
% Display only Classes
for i = 1:types.Length
if types(i).IsClass
disp(char(types(i).FullName));
end
end
Here is an example of the output from the above code. 
FxResources.System.Collections.SR
System.SR
System.Collections.BitArray
System.Collections.BitArray+BitArrayEnumeratorSimple
System.Collections.ThrowHelper
System.Collections.StructuralComparisons
System.Collections.StructuralEqualityComparer
System.Collections.StructuralComparer
System.Collections.HashHelpers
System.Collections.ObjectModel.CollectionHelpers
System.Collections.ObjectModel.ReadOnlySet`1
System.Collections.Generic.ICollectionDebugView`1
System.Collections.Generic.IDictionaryDebugView`2
...
Note that the the notation "`1" indicates a generic type with one generic argument. If you want to filter out generic types, you could change the logic in the above code to say  "if types(i).IsClass && ~types(i).IsGenericType".

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!