This codes directly against the feature.
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;
}
This code assumes the following using statements.
using System;
using System.Reflection;
using System.Security.Policy;
And if this will be called frequently you could avoid the reflection performance hit with something like this.
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;
}
Edit: I had to see for myself what the performance statistics would be, And this is what I got.
Elapsed time in milliseconds:
Catch Exception: 45331
Reflection: 58
Static Reflection: 1
Here is the code I used to do the benchmarking:
public static void BenchmarkLoaders()
{
Stopwatch timer = new Stopwatch();
// Benchmark catching Exceptions
timer.Start();
for (int i = 0; i < 10000; i++)
{
NotSupported notSupported = new NotSupported();
try
{
notSupported.ThrowException("Obsoleted Method Call");
}
catch (NotSupportedException nse)
{
//Do something
}
}
timer.Stop();
Console.WriteLine("Catch Exception: {0}", timer.ElapsedMilliseconds);
timer.Reset();
// Benchmark Reflection
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();
// Benchmark Static Reflection
timer.Start();
for (int i = 0; i < 10000; i++)
{
NotSupported.ReflectAssemblyStatic();
}
timer.Stop();
Console.WriteLine("Static Reflection: {0}", timer.ElapsedMilliseconds);
timer.Reset();
}
This is the NotSupported
class.
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)))
{
// Do something
}
}
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)
{
//Do Stuff
}
}
}
I realize that these aren't real world numbers but it does provide a very compelling argument for the use of reflection over exceptions.