Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>FWIW I implemented a system like this:</p> <pre><code> [CrmAttribute("firstname")] public string FirstName { get { return GetPropValue&lt;string&gt;(MethodBase.GetCurrentMethod().Name); } set { SetPropValue(MethodBase.GetCurrentMethod().Name, value); } } // this is in a base class, skipped that bit for clairty public T GetPropValue&lt;T&gt;(string propName) { propName = propName.Replace("get_", "").Replace("set_", ""); string attributeName = GetCrmAttributeName(propName); return GetAttributeValue&lt;T&gt;(attributeName); } public void SetPropValue(string propName, object value) { propName = propName.Replace("get_", "").Replace("set_", ""); string attributeName = GetCrmAttributeName(propName); SetAttributeValue(attributeName, value); } private static Dictionary&lt;string, string&gt; PropToAttributeMap = new Dictionary&lt;string, string&gt;(); private string GetCrmAttributeName(string propertyName) { // keyName for our propertyName to (static) CrmAttributeName cache string keyName = this.GetType().Name + propertyName; // have we already done this mapping? if (!PropToAttributeMap.ContainsKey(keyName)) { Type t = this.GetType(); PropertyInfo info = t.GetProperty(propertyName); if (info == null) { throw new Exception("Cannot find a propety called " + propertyName); } object[] attrs = info.GetCustomAttributes(false); foreach (object o in attrs) { CrmAttributeAttribute attr = o as CrmAttributeAttribute ; if (attr != null) { // found it. Save the mapping for next time. PropToAttributeMap[keyName] = attr.AttributeName; return attr.AttributeName; } } throw new Exception("Missing MemberOf attribute for " + info.Name + "." + propertyName + ". Could not auto-access value"); } // return the existing mapping string result = PropToAttributeMap[keyName]; return result; } </code></pre> <p>There's also a custom attribute class called CrmAttributeAttribute.</p> <p>I'd strongly recommend <strong>against</strong> using GetStackFrame() as part of your solution, my original version of the solution was originally the much neater:</p> <pre><code>return GetPropValue&lt;string&gt;(); </code></pre> <p>But it was 600x slower than the version above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload