日期:2014-05-17  浏览次数:21001 次

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))
                    {
        &nb