使用反射,我怎样才能找到接受反射库本身中定义的参数的方法?

FFMG

我有一个用 .NET461 编写的库,我想使用反射调用该库中的一个方法

类本身看起来像......

public class ClassA {
}
...
public class ClassB {
  public MethodA( ClassA val )
  {
    ..
  }  

  public MethodA( List<ClassA> list )
  {
    ..
  }  
}

我的调用 exe 是一个 .NET48 可执行文件...

(为简洁起见,删除了验证和一些代码)

// get the library
var asm = Assembly.LoadFrom( "...myAssembly.dll" );

// instantiate ClassB
var instance = asm.CreateInstance( "ClassB" )

// look for that class type so we can get the method "MethodA"
var type = asm.GetExportedTypes().FirstOrDefault( t => t.Name == "ClassB" ); 

// look for all the methods called "MethodA"
// this call works and I can see I have 2 methods with that name
var methods = type.GetMethods().Where(m => m.Name == "MethodA").ToList();

// look for the method just by that name.
// Here I get an error as there are 2 methods with the same name.
var method = type.GetMethod( "MethodA");

如果我传递参数,则可以获得该方法,但列表中的“类型”应该是List<ClassA>

我怎样才能找到带有参数的方法,List<ClassA>ClassA它本身是在库中定义的而不是在我的可执行文件中时。

...
var methodA = type.GetMethod("MethodA", new []
          {
            typeof(List<ClassA>>)                  // <<<< ClassA is not defined here. 
                                                   //      It is defined in the library
          } );
罗马

您可以获取与获取类型变量ClassA相同的方式,ClassB然后构造参数类型并获取所需的方法:

var asm = Assembly.LoadFrom( "...myAssembly.dll" );
var assemblyTypes = asm.GetExportedTypes();

var classAType = assemblyTypes.FirstOrDefault( t => t.Name == "ClassA" );
var classBType = assemblyTypes.FirstOrDefault( t => t.Name == "ClassB" );

var instance = asm.CreateInstance( "ClassB" ); // or Activator.CreateInstance(classBType);

// construct parameter type - List<ClassA> type

var listOpenGenericType = typeof(List<>);

// List<ClassA>
var listClosedGenericType = listOpenGenericType.MakeGenericType(classAType); 

// find the method
var method = classBType
    .GetMethods()
    .Where(m => m.Name == "MethodA")
    .FirstOrDefault(m => 
    {
        var parameters = m.GetParameters();
                                     
        return parameters.Length == 1 && parameters.First().ParameterType == listClosedGenericType);
    });

if (method != null)
{
    // call method
    method.Invoke(instance, yourList); // pass list
}

// ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我怎样才能有一个接受“我的类型”类型参数的抽象方法?

我怎样才能得到 glm::mat4 的镜面反射?

我怎样才能找到数组中的特定项目?

我怎样才能找到div的孩子?

我怎样才能减少这些方法中的重复

如何使用反射调用接受 Func<> 参数的方法?

我怎样才能验证主要方法

我可以使用强类型反射找到具有通用参数的方法吗?

使用 clap 的 #[derive(Parser)],我怎样才能接受 std::time::Duration?

我怎样才能找到我的包裹?

我怎样才能找到我最贵的产品?

我们怎样才能挽救JSP文件使用数据库中的春天启动的表单数据?

我怎样才能全部使用老虎钳值反之亦然列从数据库MySQL中

我怎样才能找到文本中的元素,但是硒中的数字

我怎样才能让我的Ubuntu从给定的源中寻找一个库

时间和坐标:我怎样才能找到这个结果?

我怎样才能找到和计算重复?

我怎样才能找到最大值

我怎样才能从URL参数的Android?

我怎样才能使HashSet作为HashMap的参数?

我怎样才能制作这个库的ER图?

我怎样才能将自定义图像输入到这个模型中?

我怎样才能在MongoDB中使用Spring数据继承的字段定义索引?

我怎样才能“clipToBottomBounds”?

我怎样才能使我的函数在R中逆?

我怎样才能从Java中的方法调用方类对象?

我怎样才能证明Ruby的for循环实际上是使用each方法实现的?

使用反射识别方法中的Func <>参数

我怎样才能让这个 .slideToggle 方法工作?