主要内容

使用反射显示。net泛型方法

showGenericMethods函数

showGenericMethods函数读取。net对象或完全限定类名,并返回给定类或对象中泛型方法名称的单元格数组。创建下面的MATLAB®功能:

函数输出= showGenericMethods(输入)%如果输入是一个。net对象,获取MethodInfo[]如果IsNetObject(input) methods = GetType.GetMethods(input);%如果输入是字符串,获取类型并获取get MethodInfo[]elseifischar(input) && ~isempty(input) type = getType(input);如果isempty(类型)disp (strcat(输入、“未找到”))返回结束methods = GetMethods(type);其他的返回结束%从MethodInfo中生成通用方法名[]output = populateGenericMethods(methods);结束
函数output = populateGenericMethods(methods)%从MethodInfo中生成通用方法名[]Index = 1;I = 1:方法。长度方法=方法(i);如果方法。IsGenericMethod输出{index,1} = method.ToString.char;Index = Index + 1;结束结束结束
函数result = IsNetObject(输入)%必须是System的子类。对象设置为。net对象结果= isa(输入,的系统。对象的);结束
函数outputType = getType(输入)% Input是表示类名的字符串首先尝试类型句柄的静态GetType方法。此方法可以从中找到任何类型%系统或mscorlib程序集outputType = System.Type.GetType(输入,false,false);如果isempty (outputType)框架获取类型的方法失败。%手动查找每个程序集在MATLAB中可见assemblies = System.AppDomain.CurrentDomain.GetAssemblies;I = 1:组件。长度asm = assemblies.Get(i-1);在程序集中查找特定类型outputType = GetType(asm,input,false,false);如果~ isempty (outputType)找到类型-完成打破结束结束结束结束

显示类中的泛型方法

NetDocGeneric程序集包含具有泛型方法的类。

dllPath = fullfile(“c:”‘工作’“NetDocGeneric.dll”);asm = . NET.addAssembly(dllPath);asm。类
ans = 'NetDocGeneric. 'SampleClass”

中显示方法SampleClass

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

在泛型类中显示泛型方法

NetDocGeneric程序集包含具有泛型方法的泛型类。

dllPath = fullfile(“c:”‘工作’“NetDocGeneric.dll”);asm = . NET.addAssembly(dllPath);asm。GenericTypes
ans = 'NetDocGeneric. 'SampleGenericClass`1[T]'

中显示方法SampleGenericClass

cls = NET.createGeneric()“NetDocGeneric。SampleGenericClass”, {的系统。两倍的});showGenericMethods (cls)
ans = '系统。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)'

相关的话题