Find log4net logfiles at runtime without adding dependency to project I was searching for a way to get all active log4net logfiles (if any) at runtime without adding a reference to the project. 

This is the solution I came up with: 

private static IEnumerable&lt;AbstractReportInformation&gt; GetLog4NetLogfiles()
{
    var type = Type.GetType("log4net.LogManager,log4net");
    if(null == type)
        return new List&lt;AbstractReportInformation&gt;();

    dynamic[] repositories = type.GetMethod("GetAllRepositories").Invoke(null, null) as dynamic[];

    IEnumerable&lt;FileInfo&gt; files = repositories
        .SelectMany&lt;dynamic, dynamic&gt;(repos =&gt; repos.GetAppenders())
        .Where(appender =&gt; Type.GetType("log4net.Appender.FileAppender,log4net").IsAssignableFrom(appender.GetType()))
        .Select(appender =&gt; new FileInfo(appender.File));

    return files.Select(
        file =&gt; new FilesystemInformation(file)).Cast&lt;AbstractReportInformation&gt;().ToList();
}


It makes use of the dynamic keyword instead of doing it all with reflection. 


Are there any obvious errors in my code? 
Are there better ways to
achieve the same result?


I feel a bit unsure about leaving the safety of the otherwise strongly typed language / framework.

Update to explain why no references shall be used / why dynamic is used

The above code is used to "detect" whether log4net is present and then gathers the logfiles. The same will later be implemented for NLog and possibly other frameworks of that sort. The project I'm using this code in is a application health monitoring framework and I don't want to add a lot of references.
Well, that seems a strange solution to me. Despite the fact you don't have a formal reference in References folder of a VS project you still have a dependency on the log4net library. And if in runtime the log4net library won't be available, your code will fail. Moreover, somebody has to remember about that dependency and copy log4net.dll manually to you bin folder, so the code could be executed.

From my point of view, it is always better to have a visible dependency rather than having invisible one. The only exception comming to my mind is when you need to have a modular architecture where modules can be safely plugged and unplugged. And, as far as I can understand, it is not your requirement.

I would recommend you to create a separate project that would have a normal reference to log4net and which would be used by your main project. In that case you will have your main project unaware of log4net, but in the same time you would have all compile-time checks and every benefit of strongly typed language / framework.