GetCustomAttributes获取不到类型属性
有个项目需要调用别人C#工程的DLL,但是用GetCustomAttributes获取不到类型的属性。
下面的代码是别人C#工程中调用DLL的代码,可以正常获取到类型的属性。但是同样的代码放到我的C#工程中就不行了。接触C#还不到两周,还不熟悉。请帮忙分析一下什么情况会导致我的问题?谢谢啦
private bool LoadPlugin()
{
foreach (string fileName in Directory.GetFiles(_PluginPath.ToString()))
{
FileInfo file = new FileInfo(fileName);
if (!file.Extension.Equals(".dll")) continue;
Assembly pluginAssembly = Assembly.LoadFrom(fileName);
foreach (Type pluginType in pluginAssembly.GetTypes())
{
// Only look at public types and non-abstract types
if (!pluginType.IsPublic || pluginType.IsAbstract)
{
continue;
}
// Get a type object of the interface we need the plugins to match
Type typeInterface = pluginType.GetInterface("XXX.PlugIn.IPlugIn", true);
// Make sure the interface we want to use actually exists
if (typeInterface == null)
{
continue;
}
foreach (PlugInAttribute attribute in pluginType.GetCustomAttributes(typeof(PlugInAttribute), true))
{
if (null == attribute)
{
continue;
}
if (attribute.Application != "ABC")
{
continue;
}
IPlugIn Instance = (IPlugIn)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
typeInterface = null;
break;
}//foreach
typeInterface = null;
}//foreach
pluginAssembly = null;
}
return true;
}
[最优解释]
需要你将dll文件复制到bin\debug 文件夹下,然后声明dll类似: [DllImport("要引用dll文件的名字.dll")],然后才能使用dll
[其他解释]
regsvr32 注册DLL 然后添加引用。
[其他解释]
要调用的DLL是托管的,不需要用Dllimport吧?
[其他解释]
然后去别人机器上去看这个工程的dll引用路径 。怎么弄的查看下。
[其他解释]
我采用的是动态调用,不能预先添加引用。
现在加载dll成功了,type也获取到了,只是得不到type的属性。
[其他解释]
堵在这里好几天了,各位好汉,帮帮俺吧
哪位提供有价值线索的200分相赠,直接解决问题的500分相赠
[其他解释]
PlugInAttribute这个类定义是写在哪里的?应该要在一个引入的dll中,不能是写在你的项目里的代码。
[其他解释]
jshi123应该是正确的,今天也有人告诉我是这个原因。
后面我发帖子单独给jshi123分,这个帖子里的就给上面两位了。
[其他解释]
解决了就好,散分太多要关小黑屋的,你还是赶紧把帖子删了吧。谢谢了,呵呵