c# - Why does ProvideAspects method not add the attribute to IL when instantiating it? -
i have following code attribute i'm applying class , property within class:
public class serialiseattribute : attribute, iaspectprovider, ivalidableannotation { public string applytoproperty { get; set; } public string name { get; set; } public bool ignore { get; set; } bool ivalidableannotation.compiletimevalidate(object target) { return true; } ienumerable<aspectinstance> iaspectprovider.provideaspects(object targetelement) { var type = targetelement type; if (type != null && !fastserialisationcacheattribute.appliedto.contains(type)) { fastserialisationcacheattribute.appliedto.add(type); yield return new aspectinstance(type, new fastserialisationcacheattribute()); } } }
this intialises fastserialisationcacheattribute , executes compiletimeinitialize (it derives typelevelaspect aspect). however, when check il generated; there no fastserialisationcacheattribute on type provided, nor @ runtime can find 1 using reflection.
if switch out provideraspects function code:
ienumerable<aspectinstance> iaspectprovider.provideaspects(object targetelement) { var type = targetelement type; if (type != null && !fastserialisationcacheattribute.appliedto.contains(type)) { fastserialisationcacheattribute.appliedto.add(type); var constructor = typeof(fastserialisationcacheattribute).getconstructor(bindingflags.instance | bindingflags.public, null, type.emptytypes, null); var objectconstruction = new objectconstruction(constructor); var introducecacheaspect = new customattributeintroductionaspect(objectconstruction); yield return new aspectinstance(type, introducecacheaspect); } }
then adds attribute il, doesn't initialise attribute (execute compiletimeinitialize).
well, think you're close solution in question. if want introduce both aspect , attribute dynamically need return instances of both fastserialisationcacheattribute
, customattributeintroductionaspect
provideaspects
method.
the method iaspectprovider.provideaspects
executed after applied aspect attributes have been read assembly. if introduce attribute in second example it's late cause aspect introduction.
in iaspectprovider.provideaspects
introduce additional aspects target. if need introduce actual attribute, use customattributeintroductionaspect
.
note adding attribute not needed aspect work, don't know logic in code.
Comments
Post a Comment