Index: src/Util/SystemInfo.cs =================================================================== --- src/Util/SystemInfo.cs (revision 1513643) +++ src/Util/SystemInfo.cs (working copy) @@ -453,6 +453,10 @@ { return "Global Assembly Cache"; } + if (AssemblyIsDynamic(myAssembly)) + { + return "Dynamic Assembly"; + } else { try @@ -462,6 +466,12 @@ // carry on. return myAssembly.Location; } + catch (NotSupportedException) + { + // This code should be unreachable, since we are checking + // whether the assembly is dynamic above. But maybe we have missed a case. + return "Probably dynamic assembly"; + } catch (ArgumentException ex) { return "Location Detect Failed (" + ex.Message + ")"; @@ -474,6 +484,30 @@ #endif } + private static bool AssemblyIsDynamic(Assembly assembly) + { +#if NET_4_0 + return assembly.IsDynamic; +#else + if (assembly is System.Reflection.Emit.AssemblyBuilder) + { + return true; + } + try + { + if (assembly.GetType().FullName == "System.Reflection.Emit.InternalAssemblyBuilder") + { + return true; + } + } + catch (TargetInvocationException) + { + } + + return false; +#endif + } + /// /// Gets the fully qualified name of the , including /// the name of the assembly from which the was Index: tests/src/Util/SystemInfoTest.cs =================================================================== --- tests/src/Util/SystemInfoTest.cs (revision 1513643) +++ tests/src/Util/SystemInfoTest.cs (working copy) @@ -23,6 +23,11 @@ using NUnit.Framework; +#if NET_4_0 +using System.Linq.Expressions; +using System.Reflection; +#endif + namespace log4net.Tests.Util { /// @@ -31,7 +36,39 @@ [TestFixture] public class SystemInfoTest { +#if NET_4_0 + /// + /// It's "does not throw not supported exception" NOT + /// "returns 'Dynamic Assembly' string for dynamic assemblies" by purpose. + /// can be JITted and inlined in different release configurations, + /// thus we cannot determine what the exact result of this test will be. + /// In 'Debug' GetCallingAssembly should return dynamic assembly named: 'Anonymously Hosted DynamicMethods Assembly' + /// whereas in 'Release' this will be inlined and the result will be something like 'X:\Y\Z\log4net.Tests.dll'. + /// Therefore simple check against dynamic assembly + /// in to avoid 'Debug' release. + /// [Test] + public void TestAssemblyLocationInfoDoesNotThrowNotSupportedExceptionForDynamicAssembly() + { + var systemInfoAssemblyLocationMethod = GetAssemblyLocationInfoMethodCall(); + + Assert.DoesNotThrow(() => systemInfoAssemblyLocationMethod()); + } + + private static Func GetAssemblyLocationInfoMethodCall() + { + var method = typeof(SystemInfoTest).GetMethod("TestAssemblyLocationInfoMethod", new Type[0]); + var methodCall = Expression.Call(null, method, new Expression[0]); + return Expression.Lambda>(methodCall, new ParameterExpression[0]).Compile(); + } + + public static string TestAssemblyLocationInfoMethod() + { + return SystemInfo.AssemblyLocationInfo(Assembly.GetCallingAssembly()); + } +#endif + + [Test] public void TestGetTypeFromStringFullyQualified() { Type t;