Display .NET Generic Methods Using Reflection

showGenericMethods Function

The showGenericMethods function, reads a .NET object or a fully qualified class name and returns a cell array of the names of the generic method in the given class or object. Create the following MATLAB® functions:

function output = showGenericMethods(input)
% if input is a .NET object, get MethodInfo[]
if IsNetObject(input)
    methods = GetType.GetMethods(input);
    % if input is a string, get the type and get get MethodInfo[]
elseif ischar(input) && ~isempty(input)
    type = getType(input);
    if isempty(type)
        disp(strcat(input,' not found'))
        return
    end
    methods = GetMethods(type);
else
    return
end
% generate generic method names from MethodInfo[]
output = populateGenericMethods(methods);

end
function output = populateGenericMethods(methods)
% generate generic method names from MethodInfo[]
index = 1;
for i = 1:methods.Length
    method = methods(i);
    if method.IsGenericMethod
        output{index,1} = method.ToString.char;
        index = index + 1;
    end
end
end
function result = IsNetObject(input)
% Must be sub class of System.Object to be a .NET object
result = isa(input,'System.Object');
end
function outputType = getType(input)
% Input is a string representing the class name
% First try the static GetType method of Type handle.
% This method can find any type from 
% System or mscorlib assemblies
outputType = System.Type.GetType(input,false,false);
if isempty(outputType)
    % Framework's method to get the type failed.
    % Manually look for it in
    % each assembly visible to MATLAB
    assemblies = System.AppDomain.CurrentDomain.GetAssemblies;
    for i = 1:assemblies.Length
        asm = assemblies.Get(i-1);
        % Look for a particular type in the assembly
        outputType = GetType(asm,input,false,false);
        if ~isempty(outputType)
            % Found the type - done
            break
        end
    end
end
end

Display Generic Methods in a Class

The NetDocGeneric assembly contains a class with generic methods.

dllPath = fullfile('c:','work','NetDocGeneric.dll');
asm = NET.addAssembly(dllPath);
asm.Classes
ans = 
    'NetDocGeneric.SampleClass'

Display the methods in SampleClass:

showGenericMethods('NetDocGeneric.SampleClass')
ans = 
    'K GenMethod[K](K)'
    'K GenMethodWithMixedArgs[K](K, K, Boolean)'
    'K GenStaticMethod[K](K)'
    'K GenStaticMethodWithMixedArgs[K](K, K, Boolean)'

Display Generic Methods in a Generic Class

The NetDocGeneric assembly contains a generic class with generic methods.

dllPath = fullfile('c:','work','NetDocGeneric.dll');
asm = NET.addAssembly(dllPath);
asm.GenericTypes
ans = 
    'NetDocGeneric.SampleGenericClass`1[T]'

Display the methods in SampleGenericClass:

obj = NET.createGeneric('NetDocGeneric.SampleGenericClass',...
          {'System.Double'});
showGenericMethods(obj)
ans = 
    'System.String ParameterizedGenMethod[K](Double, K)'
    'T GenMethod[T](T)'
    'K GenStaticMethod[K](K)'
    'K GenStaticMethodWithMixedArgs[K](K, K, Boolean)'
    'System.String ParameterizedStaticGenMethod[K](Double, K)'
Was this topic helpful?