首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在没有Assembly.GetCallingAssembly()的情况下获取包含接口的程序集

在没有Assembly.GetCallingAssembly()的情况下,可以通过其他方法获取包含接口的程序集。以下是一种可能的解决方案:

  1. 使用反射获取当前执行的方法的类型。Type currentType = MethodBase.GetCurrentMethod().DeclaringType;
  2. 通过当前类型获取所在的程序集。Assembly currentAssembly = currentType.Assembly;
  3. 遍历程序集中的所有类型,找到包含接口的类型。Type interfaceType = typeof(IYourInterface); Type implementingType = currentAssembly.GetTypes().FirstOrDefault(t => interfaceType.IsAssignableFrom(t));
  4. 如果找到了包含接口的类型,可以进行后续操作,如实例化对象或调用方法。if (implementingType != null) { // 实例化对象 object instance = Activator.CreateInstance(implementingType); // 调用方法 MethodInfo method = implementingType.GetMethod("YourMethod"); method.Invoke(instance, null); }

这种方法可以在没有Assembly.GetCallingAssembly()的情况下获取包含接口的程序集,并进行后续操作。请注意,这只是一种解决方案,具体实现可能因编程语言和环境而异。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券