c# - GetKey in ConfigurationElementCollection from ConfigurationPropertyAttribute? -


there lot of examples out there explain how create own configurationelementcollection, instance: stackoverflow: how implement own configurationelementcollection?

one of functions you'll have override getelementkey:

protected override object getelementkey(configurationelement element) {   return ((serviceconfig) element).port; } 

where property port defined follows:

[configurationproperty("port", isrequired = true, iskey = true)] public int port  {   { return (int) this["port"]; }   set { this["port"] = value; } } 

my configuration has several configurationelementcollections similar. getelementkey function function inhibits use of generic configurationelementcollection because of identifier of key. configurationpropertyattribute informs me property key.

is possible key property via configurationpropertyattribute?

code like:

public class configcollection<t> : configurationelementcollection t: configurationelement, new() {     protected override object getelementkey(configurationelement element)     {         // propertyinfo of property has iskey = true         propertyinfo keypropertyinfo = ...          object keyvalue = keypropertyinfo.getvalue(element);         return keyvalue;      } 

yes, can properties of element , 1 has configurationpropertyattribute iskey == true:

protected override object getelementkey(configurationelement element) {     object key = element.gettype()                         .getproperties()                         .where(p => p.getcustomattributes<configurationpropertyattribute>()                                      .any(a => a.iskey))                         .select(p => p.getvalue(element))                         .firstordefault();      return key; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -