How to get current property name via reflection?

后端 未结 8 766
悲哀的现实
悲哀的现实 2020-12-02 16:54

I would like to get property name when I\'m in it via reflection mechanism. Is it possible?

Update: I have code like this:

    public CarType Car
            


        
8条回答
  •  臣服心动
    2020-12-02 17:26

    FWIW I implemented a system like this:

        [CrmAttribute("firstname")]
        public string FirstName
        {
           get { return GetPropValue(MethodBase.GetCurrentMethod().Name); }
           set { SetPropValue(MethodBase.GetCurrentMethod().Name, value); }
        }
    
        // this is in a base class, skipped that bit for clairty
        public T GetPropValue(string propName)
        {
            propName = propName.Replace("get_", "").Replace("set_", "");
            string attributeName = GetCrmAttributeName(propName);
            return GetAttributeValue(attributeName);            
        }
    
        public void SetPropValue(string propName, object value)
        {
            propName = propName.Replace("get_", "").Replace("set_", "");
            string attributeName = GetCrmAttributeName(propName);
            SetAttributeValue(attributeName, value);
        }
    
        private static Dictionary PropToAttributeMap = new Dictionary();
        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;
            }
    

    There's also a custom attribute class called CrmAttributeAttribute.

    I'd strongly recommend against using GetStackFrame() as part of your solution, my original version of the solution was originally the much neater:

    return GetPropValue();
    

    But it was 600x slower than the version above.

提交回复
热议问题