After many attempts, if found the solution, which is very easy:
routes.MapRoute("Defaults_Route" ,
"" ,
new { area = "MyArea" , controller = "Home" , action = "Index" }
).DataTokens.Add("area" , "MyArea" );
routes.MapRoute("Defaults_Route" ,
"" ,
new { area = "MyArea" , controller = "Home" , action = "Index" }
).DataTokens.Add("area" , "MyArea" );
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var vio = new SomeVeryImportantObject { VeryImportantProperty = "Top Secret" };
var moreImportantObject = new NeedInfoToWork();
moreImportantObject.SetAnothersPropertyToUseLaterUsingDelegates(() => vio.VeryImportantProperty);
moreImportantObject.SetAnothersPropertyToUseLaterUsingReflection(vio, "VeryImportantProperty");
vio.VeryImportantProperty = "Ok, not so important";
Console.WriteLine(moreImportantObject.GetValueNowUsingReflection());
Console.WriteLine(moreImportantObject.GetValueUsingClousers());
const int timesToAccess = 1000000;
var sw = Stopwatch.StartNew();
for (int i = 0; i < timesToAccess; i++)
{
var value = moreImportantObject.GetValueNowUsingReflection();
}
sw.Stop();
Console.WriteLine("Time Taken using reflection {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < timesToAccess; i++)
{
var value = moreImportantObject.GetValueUsingClousers();
}
sw.Stop();
Console.WriteLine("Time Taken using closures {0}ms", sw.ElapsedMilliseconds);
Console.ReadKey();
}
}
public class SomeVeryImportantObject
{
public string VeryImportantProperty { get; set; }
}
public class NeedInfoToWork
{
private Func_propertyAccess;
private object _objectToAccess;
private string _propertyName;
public void SetAnothersPropertyToUseLaterUsingDelegates(FuncpropertyAccess)
{
_propertyAccess = propertyAccess;
}
public void SetAnothersPropertyToUseLaterUsingReflection(Object objectToGetPropertyValueFrom, string propertyName)
{
_objectToAccess = objectToGetPropertyValueFrom;
_propertyName = propertyName;
}
public string GetValueNowUsingReflection()
{
return _objectToAccess.GetType()
.GetProperty(_propertyName)
.GetValue(_objectToAccess, null)
.ToString();
}
public string GetValueUsingClousers()
{
return _propertyAccess();
}
}
}
01 yield return () => SomeStringProperty;
01 generatedProperty.GetStatements.Add(new CodeSnippetExpression(string.Format("yield return () => {0}", existingProperty.Name)));
1 type.CustomAttributes.Add( 2 new CodeAttributeDeclaration(new CodeTypeReference(typeof (RangeAttribute)),
3 new CodeAttributeArgument("Min",
4 new CodePrimitiveExpression("1"))));
1 var enumTypeExpression = new CodeTypeReferenceExpression(typeof(Format));
2 var enumValueExpression = new CodeFieldReferenceExpression(enumTypeExpression, Format.Bool.ToString());
3 4 type.CustomAttributes.Add( 5 new CodeAttributeDeclaration(
6 new CodeTypeReference(typeof (FormatAttribute)),
7 new CodeAttributeArgument("Format", enumValueExpression)));
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "TOKEN_VERSION")] 1 var assembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "System.Xml");
2
3 var version = FileVersionInfo.GetVersionInfo(new Uri(assembly.CodeBase).AbsolutePath).ProductVersion;