.
public static Assembly LoadAssembly(string assembly, Evidence evidence)
{
Assembly asm;
MethodInfo load =
typeof(Assembly).GetMethod("Load",
new Type[] {typeof(string), typeof(Evidence)});
if (Attribute.IsDefined(load, typeof(ObsoleteAttribute)))
{
asm = Assembly.Load(assembly);
}
else
{
asm = Assembly.Load(assembly, evidence);
}
return asm;
}
.
using System;
using System.Reflection;
using System.Security.Policy;
, - .
private static bool? _isEvidenceObsolete = null;
public static Assembly AssemblyLoader(string assembly, Evidence evidence)
{
Assembly asm;
if (!_isEvidenceObsolete.HasValue)
{
MethodInfo load =
typeof(Assembly).GetMethod("Load",
new Type[] { typeof(string), typeof(Evidence) });
_isEvidenceObsolete = Attribute.IsDefined(load, typeof(ObsoleteAttribute));
}
if (_isEvidenceObsolete.Value)
{
asm = Assembly.Load(assembly);
}
else
{
asm = Assembly.Load(assembly, evidence);
}
return asm;
}
: , . , .
:
Catch Exception: 45331
Reflection: 58
Static Reflection: 1
, :
public static void BenchmarkLoaders()
{
Stopwatch timer = new Stopwatch();
timer.Start();
for (int i = 0; i < 10000; i++)
{
NotSupported notSupported = new NotSupported();
try
{
notSupported.ThrowException("Obsoleted Method Call");
}
catch (NotSupportedException nse)
{
}
}
timer.Stop();
Console.WriteLine("Catch Exception: {0}", timer.ElapsedMilliseconds);
timer.Reset();
timer.Start();
for (int i = 0; i < 10000; i++)
{
NotSupported notSupported = new NotSupported();
notSupported.ReflectAssembly();
}
timer.Stop();
Console.WriteLine("Reflection: {0}", timer.ElapsedMilliseconds);
timer.Reset();
timer.Start();
for (int i = 0; i < 10000; i++)
{
NotSupported.ReflectAssemblyStatic();
}
timer.Stop();
Console.WriteLine("Static Reflection: {0}", timer.ElapsedMilliseconds);
timer.Reset();
}
NotSupported.
public class NotSupported
{
public void ThrowException(string message)
{
throw new NotSupportedException(message);
}
public void ReflectAssembly()
{
MethodInfo load =
typeof(Assembly).GetMethod("Load",
new Type[] { typeof(string), typeof(Evidence) });
if (Attribute.IsDefined(load, typeof(ObsoleteAttribute)))
{
}
}
private static bool? _isEvidenceObsolete = null;
public static void ReflectAssemblyStatic()
{
Assembly asm;
if (!_isEvidenceObsolete.HasValue)
{
MethodInfo load =
typeof(Assembly).GetMethod("Load",
new Type[] { typeof(string), typeof(Evidence) });
_isEvidenceObsolete = Attribute.IsDefined(load, typeof(ObsoleteAttribute));
}
if (_isEvidenceObsolete.Value)
{
}
}
}
, , .