简体   繁体   English

动态加载的类上缺少属性

[英]Missing attributes on dynamically loaded class

I am using Structure Map to load plugins from a child directory. 我正在使用结构图从子目录加载插件。

Both the main app and the plugin reference the FileHelpers dll. 主应用程序和插件都引用FileHelpers dll。 FileHelpers has attributes that you put on a class to define what the record is delimited by. FileHelpers具有您放置在类上的属性,以定义记录的边界。 These are defined in my plugin. 这些在我的插件中定义。 eg. 例如。

[Delimited('\t')] 
public class Test {
    public string name;
}

The FileHelpers utitlity is run from the main app using the class definitions provided by the plugins. FileHelpers实用程序是使用插件提供的类定义从主应用程序运行的。 If I put the plugin dll in a directory underneath the main application then I get an issue with the FileHelpers library complaining that the attribute cannot be found, however if it is placed next to the main library (same folder), then it works fine. 如果我将插件dll放在主应用程序下的目录中,则会遇到FileHelpers库的问题,抱怨找不到该属性,但是如果将该属性放置在主库(相同的文件夹)旁边,则可以正常工作。

I have placed some further debug statements into my code and have found that if 我在代码中放了一些其他的调试语句,发现如果

var type = typeof(Test);
var attributes = type.GetCustomAttributes(true); 

is used and not the specific (the one FileHelpers is using) 使用而不是特定的(一个FileHelpers正在使用)

var attributes = type.GetCustomAttributes(typeof(DelimitedAttribute), true);

then it finds the custom attributes without any troubles. 然后它将找到自定义属性,而不会遇到任何麻烦。

I thought this may have been a SM thing but have tried MEF and by doing it using Assembly.Load() and the same thing happens. 我以为这可能是SM事情,但是尝试了MEF并通过使用Assembly.Load()进行了同样的事情。

I think you are running into the issue described here . 我认为您遇到了此处描述的问题。

According to the blog post linked in the answer , it looks like the plugin dll would need to be strongly named and fully trusted, otherwise GetCustomAttributes will filter out the DelimitedAttribute . 根据答案中链接的博客文章,似乎插件dll需要被强命名并被完全信任,否则GetCustomAttributes将过滤出DelimitedAttribute You could try to add the AllowPartiallyTrustedCallers attribute to the plugin assembly. 您可以尝试将AllowPartiallyTrustedCallers属性添加到插件程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM