.net - C# - library to pass lambda into attribute (aspect) constructor -


for mapping purposes pass func<> attribute constructor, become possible:

[map<foo>(foo=>foo.name)] public string name { get; set; } 

i realize attributes have compile-time constant, impossible pure c#.

i wonder if postsharp-like library exists similar (judging this post guess there ways implement in pure il).

no way. want 2 things attributes doesn't support in c#:

  • generic type parameters.
  • non-constant arguments.

think attributes metadata, can't expect run nothing during usage (obviously attribute constructor may run whichever code may add on it).

while other q&a you've linked mentions emitting il directly should able it, i'm pretty sure can avoid adding complexity project implementing fluent api:

public class : imappingconfigurator {      public string name { get; set; }       public void configure(imappingconfiguration config)      {           config.map<foo>(foo => foo.name);      }  } 

and during configuration stage, can types implementing whole interface , instantiate them configuration of each one:

ienumerable<imappingconfigurator> configurators = assembly.getexecutingassembly().gettypes().where (     t => t.getinterfaces().any(t => t == typeof(imappingconfigurator)) ).select(t => (imappingconfigurator)activator.createinstance(t)); 

Comments