c# - How can I pass a runtime method to a custom attribute or viable alternative approach -
basically @ top of class (i know doesn't work isn't constant)....
[xmlstorage(issinglestoragefile = false, issubordinate = true, storagelocation = "posts" + getblogid()]
where getblogid() static utility method.
i'm building xml storage framework blogging engine writing (partly learning excercise, partly want give open source) , thought tidiest way determine storage location use custom attributes since use datacontractserializer anyway.
my problem @ present determining location subordinate type location determined id of parent. e.g post < blog.
my storage path this...
posts\blogid\postid.xml
where blog id determined parsing url , returning associated blog. allow me host multiple blogs in 1 installation whilst keeping post storage files separate reduce memory overheads when loading posts.
is straight no or there better way me attempting?
edit:
following john answer tried this....
private static string getsubordinatepath(type type) { if (typeof(isubordinate).isassignablefrom(type)) { object instance = activator.createinstance(type); return (instance isubordinate).parentguid.tostring(); } else { // todo: localize this. throw new argumentexception( string.format( cultureinfo.currentculture, "the specified type '{0}' not impliment isubordinate interface. please edit source appropriately enable storage.", type.gettype().name)); } }
which called class reading custom attribute.
this works nicely..
it's straight no attributes... values constants baked metadata.
one option could use have sort of templating built whatever uses attributes... have storage location of posts\{getblogid()}
, call method @ execution time. it's not elegant though... might want consider using interface instead.
Comments
Post a Comment