c# - Extension Method for Custom Attributes -
i working asp.net mvc4 website , have model & view model layer. because of reasons have different names few properties in model , viewmodel
model
public partial class project { public string desc {get; set;} }
view model
public class projectviewmodel { public string description { get; set; } }
now @ model layer, need use viewmodel name of property if different. thinking of creating custom attribute can have in models:
public partial class project { [viewmodelpropertyname("description")] public string desc {get;set;} }
and use @ model layer
string.format("viewmodel property name {0}", this.desc.viewmodelpropertyname())
i want generic if there no viewmodelpropertyname
attribute on property should return same property name i.e. if desc
property has no attribute should return "desc"
only.
here tried
public class viewmodelpropertynameattribute : system.attribute { #region fields string viewmodelpropertyname; #endregion #region properties public string getviewmodelpropertyname() { return viewmodelpropertyname; } #endregion #region constructor public viewmodelpropertynameattribute(string propertyname) { this.viewmodelpropertyname = propertyname; } #endregion }
need how access custom attribute
current state
public static class modelextensionmethods { public static string viewmodelpropertyname(this object obj) { // error: cannot convert 'object' 'system.reflect.assembly' system.attribute[] attrs = system.attribute.getcustomattributes(obj); foreach (system.attribute attr in attrs) { if (attr viewmodelpropertynameattribute) { return ((viewmodelpropertynameattribute)attr).getviewmodelpropertyname(); } } return string.empty; } }
but has compile time error:
unfortunately can not attributes used decorate properties reflecting on type of property itself. have therefore modified viewmodelpropertyname(this object) extension method take in name of desired property.
this method take in name of property attribute wish get. if attribute exists return value passed constructor, if on other hand, not exist return name of property passed in.
public static class modelextensionmethods { public static string viewmodelpropertyname(this object obj, string name) { var attributes = obj.gettype() .getcustomattributes(true) .oftype<metadatatypeattribute>() .first() .metadataclasstype .getproperty(name) .getcustomattributes(true); if (attributes.oftype<viewmodelpropertynameattribute>().any()) { return attributes.oftype<viewmodelpropertynameattribute>() .first() .getviewmodelpropertyname(); } else { return name; } } }
you can define following classes test new approach.
[metadatatype(typeof(testclassmeta))] class testclass { } class testclassmeta { [viewmodelpropertyname("thename")] public string firstname { get; set; } public string lastname { get; set; } }
also, can see following lines of code, viewmodelpropertyname(this object, string) extension method called on instance of testclass, instead of calling on property itself.
class program { static void main() { console.writeline(new testclass().viewmodelpropertyname("firstname")); console.writeline(new testclass().viewmodelpropertyname("lastname")); console.read(); } }
Comments
Post a Comment