简体   繁体   中英

Reflection - trouble with loading dependent assembly

I am trying to execute below code to see if assembly was built in Debug or Release mode.

Assembly assemb = Assembly.LoadFile(fileName);
bool isDebug = false;
foreach (object att in assemb.GetCustomAttributes(false))
    if (att is DebuggableAttribute)
        isDebug = ((DebuggableAttribute)att).IsJITTrackingEnabled;
Console.WriteLine("Assembly is {0}.", isDebug ? "debug" : "release");

I am able to load assembly (Product.dll) without any issue. But when I am trying to execute GEtCustomAttributes(false) method I am getting below exception message.

Could not load file or assembly 'log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

Don't know why this is looking for dependent assembly. Is it because of the way Product.dll was build ( like optimization or something ). I don't have access to source code of Product.dll so not sure how I can file its mode ( Debug or Release )

Typically dependent DLLs are not compiled into the resulting DLL. This means that if Product.dll is compiled with a dependency to log4net.dll , it has to be in the same folder.
It should be possible to simply copy log4net.dll into the same folder where Product.dll is located.

The dependency must be loaded if a type defined in Product.dll references a type defined in log4net.dll . In this case I strongly suspect that it will be Logger / ILog , because this line is often included to get the Logger .

private static readonly log4net.ILog log = log4net.LogManager.GetLogger();

If log4net is a mandatory (meaning directly referenced) assembly for your referenced project you have to also load it, there is no way to only load parts of an assembly. Having said this the code for the referenced assembly won´t probably won´t even compile if log4net.dll is missing so it HAS to load it in any way.

Anyway obiosly any of your attributes defined in the referenced assembly needs the Logger from log4net , so it´ll search for that type.

Put the log4net assembly into your build-path and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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