nicko 2004/02/26 13:44:53
Modified: src/Util SystemInfo.cs
tests/src log4net.Tests.csproj
Added: tests/src/Util SystemInfoTest.cs
Log:
Updated SystemInfo.GetTypeFromString to search all the loaded assemblies
for an unqualified type name if it is not found in the relative assembly.
Added tests for GetTypeFromString.
Revision Changes Path
1.3 +48 -5 logging-log4net/src/Util/SystemInfo.cs
Index: SystemInfo.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Util/SystemInfo.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SystemInfo.cs 16 Feb 2004 02:10:54 -0000 1.2
+++ SystemInfo.cs 26 Feb 2004 21:44:53 -0000 1.3
@@ -28,6 +28,7 @@
/// </summary>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
+ /// <author>Alexey Solofnenko</author>
public sealed class SystemInfo
{
#region Private Instance Constructors
@@ -391,7 +392,8 @@
/// </para>
/// <para>
/// If the type name is not fully qualified, it will be loaded
from the assembly
- /// containing the specified relative type.
+ /// containing the specified relative type. If the type is not
found in the assembly
+ /// then all the loaded assemblies will be searched for the
type.
/// </para>
/// </remarks>
/// <returns>The type loaded or <c>null</c> if it could not be
loaded.</returns>
@@ -414,7 +416,8 @@
/// </para>
/// <para>
/// If the type name is not fully qualified it will be loaded
from the
- /// assembly that is directly calling this method.
+ /// assembly that is directly calling this method. If the type
is not found
+ /// in the assembly then all the loaded assemblies will be
searched for the type.
/// </para>
/// </remarks>
/// <returns>The type loaded or <c>null</c> if it could not be
loaded.</returns>
@@ -438,7 +441,8 @@
/// </para>
/// <para>
/// If the type name is not fully qualified it will be loaded
from the specified
- /// assembly.
+ /// assembly. If the type is not found in the assembly then all
the loaded assemblies
+ /// will be searched for the type.
/// </para>
/// </remarks>
/// <returns>The type loaded or <c>null</c> if it could not be
loaded.</returns>
@@ -451,12 +455,51 @@
#if NETCF
return relativeAssembly.GetType(typeName,
throwOnError);
#else
- return relativeAssembly.GetType(typeName,
throwOnError, ignoreCase);
+ // Attempt to lookup the type from the
relativeAssembly
+ Type type = relativeAssembly.GetType(typeName,
false, ignoreCase);
+ if (type != null)
+ {
+ // Found type in relative assembly
+ //LogLog.Debug("SystemInfo: Loaded type
["+typeName+"] from assembly ["+relativeAssembly.FullName+"]");
+ return type;
+ }
+
+ Assembly[] loadedAssemblies = null;
+ try
+ {
+ loadedAssemblies =
AppDomain.CurrentDomain.GetAssemblies();
+ }
+ catch(System.Security.SecurityException)
+ {
+ // Insufficient permissions to get the
list of loaded assemblies
+ }
+
+ if (loadedAssemblies != null)
+ {
+ // Search the loaded assemblies for the
type
+ foreach (Assembly assembly in
loadedAssemblies)
+ {
+ type =
assembly.GetType(typeName, false, ignoreCase);
+ if (type != null)
+ {
+ // Found type in loaded
assembly
+
LogLog.Debug("SystemInfo: Loaded type ["+typeName+"] from assembly
["+assembly.FullName+"] by searching loaded assemblies.");
+ return type;
+ }
+ }
+ }
+
+ // Didn't find the type
+ if (throwOnError)
+ {
+ throw new TypeLoadException("Could not
load type ["+typeName+"]. Tried assembly ["+relativeAssembly.FullName+"] and
all loaded assemblies");
+ }
+ return null;
#endif
}
else
{
- // Includes assembly name
+ // Includes explicit assembly name
//LogLog.Debug("SystemInfo: Loading type
["+typeName+"] from global Type");
#if NETCF
return Type.GetType(typeName, throwOnError);
1.3 +5 -0 logging-log4net/tests/src/log4net.Tests.csproj
Index: log4net.Tests.csproj
===================================================================
RCS file: /home/cvs/logging-log4net/tests/src/log4net.Tests.csproj,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- log4net.Tests.csproj 26 Feb 2004 21:08:49 -0000 1.2
+++ log4net.Tests.csproj 26 Feb 2004 21:44:53 -0000 1.3
@@ -132,6 +132,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
+ <File
+ RelPath = "Util\SystemInfoTest.cs"
+ SubType = "Code"
+ BuildAction = "Compile"
+ />
</Include>
</Files>
</CSHARP>
1.1 logging-log4net/tests/src/Util/SystemInfoTest.cs
Index: SystemInfoTest.cs
===================================================================
#region Copyright & License
//
// Copyright 2001-2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Diagnostics;
using System.Globalization;
using log4net.Config;
using log4net.Util;
using log4net.Layout;
using log4net.Core;
using log4net.Appender;
using log4net.Repository;
using log4net.Tests.Appender;
using NUnit.Framework;
namespace log4net.Tests.Util
{
/// <summary>
/// Used for internal unit testing the <see cref="SystemInfo"/> class.
/// </summary>
/// <remarks>
/// Used for internal unit testing the <see cref="SystemInfo"/> class.
/// </remarks>
[TestFixture] public class SystemInfoTest
{
[Test] public void TestGetTypeFromStringFullyQualified()
{
Type t = null;
t =
SystemInfo.GetTypeFromString("log4net.Tests.Util.SystemInfoTest,log4net.Tests",
false, false);
Assertion.AssertSame("Test explicit case sensitive type
load", typeof(SystemInfoTest), t);
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST,LOG4NET.TESTS",
false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load caps", typeof(SystemInfoTest), t);
t =
SystemInfo.GetTypeFromString("log4net.tests.util.systeminfotest,log4net.tests",
false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load lower", typeof(SystemInfoTest), t);
}
[Test] public void TestGetTypeFromStringRelative()
{
Type t = null;
t =
SystemInfo.GetTypeFromString("log4net.Tests.Util.SystemInfoTest", false, false);
Assertion.AssertSame("Test explicit case sensitive type
load", typeof(SystemInfoTest), t);
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST", false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load caps", typeof(SystemInfoTest), t);
t =
SystemInfo.GetTypeFromString("log4net.tests.util.systeminfotest", false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load lower", typeof(SystemInfoTest), t);
}
[Test] public void TestGetTypeFromStringSearch()
{
Type t = null;
t =
SystemInfo.GetTypeFromString("log4net.Util.SystemInfo", false, false);
Assertion.AssertSame("Test explicit case sensitive type
load", typeof(SystemInfo), t);
t =
SystemInfo.GetTypeFromString("LOG4NET.UTIL.SYSTEMINFO", false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load caps", typeof(SystemInfo), t);
t =
SystemInfo.GetTypeFromString("log4net.util.systeminfo", false, true);
Assertion.AssertSame("Test explicit case in-sensitive
type load lower", typeof(SystemInfo), t);
}
[Test, ExpectedException(typeof(TypeLoadException))] public
void TestGetTypeFromStringFails1()
{
Type t = null;
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST,LOG4NET.TESTS",
false, false);
Assertion.AssertSame("Test explicit case sensitive
fails type load", null, t);
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST,LOG4NET.TESTS",
true, false);
}
[Test, ExpectedException(typeof(TypeLoadException))] public
void TestGetTypeFromStringFails2()
{
Type t = null;
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST", false, false);
Assertion.AssertSame("Test explicit case sensitive
fails type load", null, t);
t =
SystemInfo.GetTypeFromString("LOG4NET.TESTS.UTIL.SYSTEMINFOTEST", true, false);
}
}
}