Skip to content

Commit

Permalink
Merge pull request #393 from Xharze/avoidSomeReflection
Browse files Browse the repository at this point in the history
Avoid uneccassary reflection
  • Loading branch information
kichristensen committed Jul 22, 2014
2 parents e5c60ec + ed9e733 commit 3a0cc40
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
16 changes: 10 additions & 6 deletions src/NLog/Config/ConfigurationItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

//

using NLog.Common;

namespace NLog.Config
{
using System;
Expand Down Expand Up @@ -187,11 +189,13 @@ public void RegisterItemsFromAssembly(Assembly assembly)
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <param name="itemNamePrefix">Item name prefix.</param>
public void RegisterItemsFromAssembly(Assembly assembly, string itemNamePrefix)
{
public void RegisterItemsFromAssembly(Assembly assembly, string itemNamePrefix)
{
InternalLogger.Debug("ScanAssembly('{0}')", assembly.FullName);
var typesToScan = assembly.SafeGetTypes();
foreach (IFactory f in this.allFactories)
{
f.ScanAssembly(assembly, itemNamePrefix);
{
f.ScanTypes(typesToScan, itemNamePrefix);
}
}

Expand Down
24 changes: 11 additions & 13 deletions src/NLog/Config/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ namespace NLog.Config
{
using System;
using System.Collections.Generic;
using System.Reflection;
using NLog.Common;
using NLog.Internal;

Expand All @@ -61,26 +60,25 @@ internal Factory(ConfigurationItemFactory parentFactory)
/// <summary>
/// Scans the assembly.
/// </summary>
/// <param name="theAssembly">The assembly.</param>
/// <param name="types">The types to scane.</param>
/// <param name="prefix">The prefix.</param>
public void ScanAssembly(Assembly theAssembly, string prefix)
public void ScanTypes(Type[] types, string prefix)
{
try
foreach (Type t in types)
{
InternalLogger.Debug("ScanAssembly('{0}','{1}','{2}')", theAssembly.FullName, typeof(TAttributeType), typeof(TBaseType));
foreach (Type t in theAssembly.SafeGetTypes())
try
{
this.RegisterType(t, prefix);
}
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
catch (Exception exception)
{
throw;
}
if (exception.MustBeRethrown())
{
throw;
}

InternalLogger.Error("Failed to add targets from '" + theAssembly.FullName + "': {0}", exception);
InternalLogger.Error("Failed to add type '" + t.FullName + "': {0}", exception);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Config/IFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal interface IFactory
{
void Clear();

void ScanAssembly(Assembly theAssembly, string prefix);
void ScanTypes(Type[] type, string prefix);

void RegisterType(Type type, string itemNamePrefix);
}
Expand Down
23 changes: 11 additions & 12 deletions src/NLog/Config/MethodFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,25 @@ internal class MethodFactory<TClassAttributeType, TMethodAttributeType> : INamed
/// and methods marked with <typeparamref name="TMethodAttributeType"/> and adds them
/// to the factory.
/// </summary>
/// <param name="theAssembly">The assembly.</param>
/// <param name="types">The types to scan.</param>
/// <param name="prefix">The prefix to use for names.</param>
public void ScanAssembly(Assembly theAssembly, string prefix)
public void ScanTypes(Type[] types, string prefix)
{
try
foreach (Type t in types)
{
InternalLogger.Debug("ScanAssembly('{0}','{1}','{2}')", theAssembly.FullName, typeof(TClassAttributeType), typeof(TMethodAttributeType));
foreach (Type t in theAssembly.SafeGetTypes())
try
{
this.RegisterType(t, prefix);
}
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
catch (Exception exception)
{
throw;
}
if (exception.MustBeRethrown())
{
throw;
}

InternalLogger.Error("Failed to add targets from '" + theAssembly.FullName + "': {0}", exception);
InternalLogger.Error("Failed to add type '" + t.FullName + "': {0}", exception);
}
}
}

Expand Down

0 comments on commit 3a0cc40

Please sign in to comment.