- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是,我从历史记录中得到的结果与我们较旧的 DLL 不同,当时变化很小,而且我不知道这些变化与显示的错误有何关联。
这是我们的 SecurityPluginServices.dll 模块中方法的一部分,它实质上是将插件添加到列表中,以便主程序可以使用它们。
它获取一组文件夹中的所有 DLL,然后针对每个 DLL 运行下面的代码。
private void AddPlugin(string FileName, LoggingUtilities.Source source)
{
//Create a new assembly from the plugin file we're adding..
Assembly pluginAssembly = Assembly.LoadFrom(FileName);
try
{
//Next we'll loop through all the Types found in the assembly
foreach (Type pluginType in pluginAssembly.GetTypes())
{
if (pluginType.IsPublic) //Only look at public types
{
if (!pluginType.IsAbstract) //Only look at non-abstract types
{
//Gets a type object of the interface we need the plugins to match
Type typeInterface = pluginType.GetInterface("SecurityInterface.ISecurityPlugin", true);
//Make sure the interface we want to use actually exists
if (typeInterface != null)
{
// Do work here
}
typeInterface = null; //Mr. Clean
}
}
}
pluginAssembly = null; //more cleanup
}
catch (ReflectionTypeLoadException ex1)
{
Console.WriteLine(ex1.Message);
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
}
}
我遇到的问题是每次到达 Type typeInterface = pluginType.GetInterface("SecurityInterface.ISecurityPlugin", true);
它总是返回空值。我需要加载的插件用于 NTLM 和 LDAP,它们在许多版本中变化很小,只添加了几个额外的属性和方法,与实现的接口(interface)无关。我已经在 ILDASM 中打开了较新的插件 DLL 和其中一个较旧的插件的副本,它们似乎都包含有关 SecurityInterface.ISecurityPlugin
的相同信息。那.GetInterface
方法正在求。
最佳答案
建议:
Type typeInterface = pluginType.GetInterface("SecurityInterface.ISecurityPlugin", true);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
将其更改为 fully-qualified type name ,即包含包含您的接口(interface)类型的程序集。
(如果您现在说在两个不同的程序集中有两个同名的不同类型,因此不能包括一个明确的程序集名称,这很可能是问题的原因。)
解释:
我的另一个回答让我怀疑是什么导致了您的问题。一般来说,typeof(T)
会强制您为包含 T
的程序集添加对项目的程序集引用。否则编译会失败。另一方面,类型名称的文本提及,例如 Type.GetType("T")
不强制执行编译时程序集引用......但它们仍然必须解析为类型实例在运行时。因此,如果省略程序集名称,则需要记住 .NET 如何将 T
映射到特定程序集。
回到你的问题。首先,请注意您没有使用完全限定的类型名称:
Type typeInterface = pluginType.GetInterface("SecurityInterface.ISecurityPlugin", true);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
您只指定命名空间和类型名称,而不是包含您的接口(interface)类型的程序集。会不会是接口(interface)类型被定义了不止一次,即在不同的程序集中,并且您提到的那个类型名称被解析为输入了错误的程序集?
让我们简要地看一下 MSDN documentation for Type.GetType(string)
method ,其中对字符串参数的描述如下:
"The assembly-qualified name of the type to get. […] If the type is in the currently executing assembly or in
Mscorlib.dll
, it is sufficient to supply the type name qualified by its namespace."
如果同样适用于您正在使用的 Type.GetInterface(string, bool)
方法怎么办?如果您当前正在执行的程序集确实包含该名称的类型怎么办?那么这就是 pluginType
将被检查的类型...但它可能是与您的插件不同的 ISecurityPlugin
接口(interface)类型(尽管名称相同)类实际实现。
关于c# - Type.GetInterface 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757298/
我刚刚发现 Type.GetInterface 和嵌套类型有一个非常奇怪的行为。 以下示例代码将显示问题,我正在使用接口(interface)的 Type.FullName 来检查给定类型是否派生自该
我创建了一个使用 System.Reflection 命名空间的项目。好吧,当我尝试使用 GetInterfaces 方法时,我发现它在 Windows 应用商店中不存在。 我尝试创建一个扩展方法,并
我尝试编写一个dll并由控制台程序调用,部分编码如下,但不知何故,我不知道为什么 getinterface 总是返回 null,然后我无法调用 dll 中的函数。 MyDLL.dll namespac
我的问题是,我从历史记录中得到的结果与我们较旧的 DLL 不同,当时变化很小,而且我不知道这些变化与显示的错误有何关联。 这是我们的 SecurityPluginServices.dll 模块中方法的
使用Hibernate ,我得到一个List ( BlablaPO 实现 Blabla )。当我做这样的事情时: for(Blabla blabla : list) { Class[] int
我会尽量简洁: 考虑界面: package com.stackoverflow; public interface Printable { void print(); } 我创建了一个实现此接口(
在 .Net 中使用反射,有什么区别: if (foo.IsAssignableFrom(typeof(IBar))) 和 if (foo.GetInterface(typeof(IBar
首先,有很多这样的问题,也许有些 OP 甚至问过这个完全相同的问题。问题是这些问题的答案(接受与否)实际上没有回答这个问题,至少我找不到。 我如何确定一个类直接声明的接口(interface),而不是
我们继承了一个设计不佳的 WCF 服务,我们希望对其进行改进。它的一个问题是它有一百多个方法(在两个不同的接口(interface)上),我们怀疑其中的大部分都没有被使用。我们决定对每个方法进行一些记
本文整理了Java中org.batfish.datamodel.Zone.getInterfaces()方法的一些代码示例,展示了Zone.getInterfaces()的具体用法。这些代码示例主要来
我最近不得不在 .NET 中进行一些反射(reflection),并且在比较泛型类型定义时偶然发现了一个奇怪的行为。 我必须决定一个类型是否是 IDictionary通过提取 Type来自 LINQ
我创建了这样一个类型: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttri
本文整理了Java中org.hippoecm.repository.api.WorkflowDescriptor.getInterfaces()方法的一些代码示例,展示了WorkflowDescrip
如果你上课scala.runtime.AbstractPartialFunction来自 Scala 2.10.2(我没有检查其他版本)并比较 AbstractPartialFunction.clas
谁能向我解释为什么下面代码中的 GetInterfaces() 会返回一个 FullName = null 的接口(interface)类型? public class Program { s
我需要从 Java 代码调用(使用 Nashorn)一个在 JavaScript 中定义的函数并向其传递一些参数。我没有使用 Invocable.invokeFunction("Foo", arg1,
我有以下代码在 Windows 7 中运行: MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
我正在尝试检测一个实现java.sql.PreparedStatement接口(interface)的类。我已经在转换方法中使用以下代码进行了尝试,但它甚至没有经过这样的类。但我正在使用包含 exec
.NET Core 1.1 支持 Type.GetInterfaces() 方法,该方法提供在给定类型上实现的接口(interface)列表。遗憾的是,Type.GetInterfaces() 在 .
我有一个插件架构,我需要检测所有实现接口(interface) ISurface 的插件任何类型(即测试 ISurface<> )。我看到这里有一些使用 LINQ 的建议(例如 this one ),
我是一名优秀的程序员,十分优秀!