private static string GetElementId(object target) { // TODO: target should implement some interface (IProvideRuntimeName with property RuntimeName) instead of relying on attributes and reflection RuntimeNamePropertyAttribute nameAttribute = target.GetType().GetCustomAttributes(typeof(RuntimeNamePropertyAttribute), true).FirstOrDefault() as RuntimeNamePropertyAttribute; return(nameAttribute != null ? (string)target.GetType().GetProperty(nameAttribute.Name).GetValue(target) : String.Empty); }
//------------------------------------------------------ // // Public Methods // //------------------------------------------------------ #region Public Methods /// <summary> /// Returns the component name. To do this, we try to find the /// RuntimeNamePropertyAttribute on the type. If we find /// the attribute, we will try to invoke the property to retrieve /// the component name. If any of these fail, we defer to the /// parent implementation. /// </summary> public string GetComponentName() { if (_instance != null) { RuntimeNamePropertyAttribute nameAttr = GetAttributes()[typeof(RuntimeNamePropertyAttribute)] as RuntimeNamePropertyAttribute; if (nameAttr != null && nameAttr.Name != null) { PropertyDescriptor nameProp = GetProperties()[nameAttr.Name]; if (nameProp != null) { return(nameProp.GetValue(_instance) as string); } } } return(_parent.GetComponentName()); }
public void RuntimeNamePropertyAttributeConstructor() { tlog.Debug(tag, $"RuntimeNamePropertyAttributeConstructor START"); try { var rnp = new RuntimeNamePropertyAttribute("Content"); Assert.IsNotNull(rnp, "Should not be null"); Assert.AreEqual("Content", rnp.Name, "Should be equal"); } catch (Exception e) { tlog.Debug(tag, e.Message.ToString()); Assert.Fail("Caught Exception : Failed!"); } tlog.Debug(tag, $"RuntimeNamePropertyAttributeConstructor END"); }
private static string GetElementId(object target) { RuntimeNamePropertyAttribute nameAttribute = target.GetType().GetCustomAttributes(typeof(RuntimeNamePropertyAttribute), true).FirstOrDefault() as RuntimeNamePropertyAttribute; return(nameAttribute != null ? (string)target.GetType().GetProperty(nameAttribute.Name).GetValue(target) : String.Empty); }