protected static string GetDefaultPropertyName(object instance) { AttributeCollection attributes = TypeDescriptor.GetAttributes(instance); DefaultPropertyAttribute defaultPropertyAttribute = ( DefaultPropertyAttribute )attributes[typeof(DefaultPropertyAttribute)]; return(defaultPropertyAttribute != null ? defaultPropertyAttribute.Name : null); }
public static void GetName() { var name = "name"; var attribute = new DefaultPropertyAttribute(name); Assert.Equal(name, attribute.Name); }
public static void Equals_SameName() { var name = "name"; var firstAttribute = new DefaultPropertyAttribute(name); var secondAttribute = new DefaultPropertyAttribute(name); Assert.True(firstAttribute.Equals(secondAttribute)); }
private void WriteProperties(XmlWriter writer, Object obj) { Type objType = obj.GetType(); string contentProperty = null; DefaultPropertyAttribute contentPropertyAttr = objType.GetCustomAttribute <DefaultPropertyAttribute>(); if (contentPropertyAttr != null) { contentProperty = contentPropertyAttr.DefaultProperty; } List <PropertyInfo> complexProperties = new List <PropertyInfo>(); foreach (PropertyInfo propertyInfo in objType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType.IsEnum || ConfigurationSerializer.SimpleTypes.Contains(propertyInfo.PropertyType)) { writer.WriteAttributeString(propertyInfo.Name, propertyInfo.GetValue(obj).ToString()); } else { complexProperties.Add(propertyInfo); } } foreach (PropertyInfo propertyInfo in complexProperties) { string propertyName = this.GetPropertyName(propertyInfo); bool isContentProperty = String.Equals(propertyName, contentProperty, StringComparison.Ordinal); if (!isContentProperty) { String propertyElementName = String.Format("{0}.{1}", this.GetElementName(obj), this.GetPropertyName(propertyInfo)); writer.WriteStartElement(propertyElementName); } if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType)) { IEnumerable items = (IEnumerable)propertyInfo.GetValue(obj); foreach (Object item in items) { Write(writer, item); } } else { Write(writer, propertyInfo.GetValue(obj)); } if (!isContentProperty) { writer.WriteEndElement(); } } }
public static PropertyInfo GetDefaultProperty(this PropertyInfo property) { if (property.PropertyType.HasAttribute <DefaultPropertyAttribute>()) { DefaultPropertyAttribute attribute = property.PropertyType.GetAttribute <DefaultPropertyAttribute>(); return(property.PropertyType.GetProperty(attribute.Name)); } return(null); }
private string GetDefaultProperty(Type objType) { DefaultPropertyAttribute contentPropertyAttr = objType.GetCustomAttribute <DefaultPropertyAttribute>(); if (contentPropertyAttr != null) { return(contentPropertyAttr.DefaultProperty); } return(null); }
private void OnPropertyGridAdornments(ITypeDescriptorContext context, PropertyDescriptor propDesc, ArrayList valueUIItemList) { IComponent component = null; IReferenceService referenceService = this.serviceProvider.GetService(typeof(IReferenceService)) as IReferenceService; if (referenceService != null) { component = referenceService.GetComponent(context.Instance); } string fullAliasName = string.Empty; //this attribue is set to overcome issue with the TypedVariableDeclarationTypeConverter //not returning Name property at all. we alias that property to the VariableDeclaration itself DefaultPropertyAttribute aliasPropertyNameAttribute = propDesc.Attributes[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute; if (aliasPropertyNameAttribute != null && aliasPropertyNameAttribute.Name != null && aliasPropertyNameAttribute.Name.Length > 0) { fullAliasName = propDesc.Name + "." + aliasPropertyNameAttribute.Name; } if (component != null) { ActivityDesigner activityDesigner = ActivityDesigner.GetDesigner(component as Activity); if (activityDesigner != null) { if (!activityDesigner.IsLocked && ActivityBindPropertyDescriptor.IsBindableProperty(propDesc) && !propDesc.IsReadOnly) { valueUIItemList.Add(new PropertyValueUIItem(DR.GetImage(DR.Bind), OnBindProperty, DR.GetString(DR.BindProperty))); } string fullComponentName = referenceService.GetName(component); //schedule1.send1 string fullPropertyName = referenceService.GetName(context.Instance); //schedule1.send1.message fullPropertyName = (fullPropertyName.Length > fullComponentName.Length) ? fullPropertyName.Substring(fullComponentName.Length + 1, fullPropertyName.Length - fullComponentName.Length - 1) + "." + propDesc.Name : string.Empty; foreach (DesignerAction action in activityDesigner.DesignerActions) { string actionPropertyName = action.PropertyName as string; if (actionPropertyName == null || actionPropertyName.Length == 0) { continue; } if (actionPropertyName == propDesc.Name || (actionPropertyName == fullPropertyName) || (actionPropertyName == fullAliasName)) { PropertyValueUIItemHandler propValueUIItemhandler = new PropertyValueUIItemHandler(action); valueUIItemList.Add(new PropertyValueUIItem(action.Image, propValueUIItemhandler.OnFixPropertyError, action.Text)); break; } } } } }
// Determine if two instances of this class are equal. public override bool Equals(Object obj) { DefaultPropertyAttribute other = (obj as DefaultPropertyAttribute); if(other != null) { return (name == other.name); } else { return false; } }
// Returns the default property on the item, or null if the item has internal static PropertyDescriptor GetDefaultProperty(ModelItem item) { DefaultPropertyAttribute propAttr = TypeDescriptor.GetAttributes(item.ItemType)[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute; if (propAttr != null && !string.IsNullOrEmpty(propAttr.Name)) { ModelProperty prop = item.Properties.Find(propAttr.Name); if (prop != null) { return(new ModelPropertyDescriptor(prop)); } } return(null); }
/// <summary> /// Returns the default property for this object. /// </summary> /// <returns>An <see cref="PSObjectPropertyDescriptor"/> that represents the default property for this object, or a null reference (Nothing in Visual Basic) if this object does not have properties.</returns> public override PropertyDescriptor GetDefaultProperty() { if (this.Instance == null) { return(null); } string defaultProperty = null; PSMemberSet standardMembers = this.Instance.PSStandardMembers; if (standardMembers != null) { PSNoteProperty note = standardMembers.Properties[TypeTable.DefaultDisplayProperty] as PSNoteProperty; if (note != null) { defaultProperty = note.Value as string; } } if (defaultProperty == null) { object[] defaultPropertyAttributes = this.Instance.BaseObject.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute), true); if (defaultPropertyAttributes.Length == 1) { DefaultPropertyAttribute defaultPropertyAttribute = defaultPropertyAttributes[0] as DefaultPropertyAttribute; if (defaultPropertyAttribute != null) { defaultProperty = defaultPropertyAttribute.Name; } } } PropertyDescriptorCollection properties = this.GetProperties(); if (defaultProperty != null) { // There is a defaultProperty, but let's check if it is actually one of the properties we are // returning in GetProperties foreach (PropertyDescriptor descriptor in properties) { if (string.Equals(descriptor.Name, defaultProperty, StringComparison.OrdinalIgnoreCase)) { return(descriptor); } } } return(null); }
/// <include file='doc\AdvancedBindingObject.uex' path='docs/doc[@for="AdvancedBindingObject.ICustomTypeDescriptor.GetProperties1"]/*' /> /// <devdoc> /// Retrieves an array of properties that the given component instance /// provides. This may differ from the set of properties the class /// provides. If the component is sited, the site may add or remove /// additional properties. The returned array of properties will be /// filtered by the given set of attributes. /// </devdoc> PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) { if (propsCollection == null) { Control control = bindings.Control; Type type = control.GetType(); PropertyDescriptorCollection bindableProperties = TypeDescriptor.GetProperties(control, attributes); AttributeCollection controlAttributes = TypeDescriptor.GetAttributes(type); DefaultPropertyAttribute defPropAttr = ((DefaultPropertyAttribute)controlAttributes[typeof(DefaultPropertyAttribute)]); ArrayList props = new ArrayList(); for (int i = 0; i < bindableProperties.Count; i++) { if (bindableProperties[i].IsReadOnly) { continue; } bool bindable = ((BindableAttribute)bindableProperties[i].Attributes[typeof(BindableAttribute)]).Bindable; DesignOnlyAttribute designAttr = ((DesignOnlyAttribute)bindableProperties[i].Attributes[typeof(DesignOnlyAttribute)]); DesignBindingPropertyDescriptor property = new DesignBindingPropertyDescriptor(bindableProperties[i], null); property.AddValueChanged(bindings, new EventHandler(OnBindingChanged)); // ASURT 45429: skip the Design time properties // if (!bindable && !showAll || designAttr.IsDesignOnly) { if (((DesignBinding)property.GetValue(this)).IsNull) { continue; } } props.Add(property); // ASURT 45429: make the default property have focus in the properties window // if (defPropAttr != null && property.Name.Equals(defPropAttr.Name)) { System.Diagnostics.Debug.Assert(this.defaultProp == null, "a control cannot have two default properties"); this.defaultProp = property; } } PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count]; props.CopyTo(propArray, 0); this.propsCollection = new PropertyDescriptorCollection(propArray); } return(propsCollection); }
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { if (_readDefaultProperty == false) { _readDefaultProperty = true; DefaultPropertyAttribute attr = _td.GetAttributes()[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute; if (attr != null && !string.IsNullOrEmpty(attr.Name)) { _defaultProperty = _typeDescriptionProvider.GetProperty(attr.Name); } } return(_defaultProperty); }
// <Snippet2> public static int Main() { // Creates a new control. MyControl myNewControl = new MyControl(); // Gets the attributes for the collection. AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewControl); /* Prints the name of the default property by retrieving the * DefaultPropertyAttribute from the AttributeCollection. */ DefaultPropertyAttribute myAttribute = (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)]; Console.WriteLine("The default property is: " + myAttribute.Name); return(0); }
static void Main(string[] args) { //Задача: //Найти все стандартные контролы, которые может использовать разработчик WinForms, //отсортировать по названию и найти для каждого DefaultProperty. Assembly WFControl = Assembly.GetAssembly(typeof(Control)); foreach (var item in WFControl.GetTypes().OrderBy(x => x.Name)) { if (item.IsSubclassOf(typeof(Control)) && item.IsAbstract == false && item.IsPublic == true) { AttributeCollection attributes = TypeDescriptor.GetAttributes(item); DefaultPropertyAttribute myAttribute = (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)]; Console.WriteLine($"Control: {item.Name}, the default property is: {myAttribute.Name}"); } } }
private void OnPropertyGridAdornments(ITypeDescriptorContext context, PropertyDescriptor propDesc, ArrayList valueUIItemList) { IComponent reference = null; IReferenceService service = this.serviceProvider.GetService(typeof(IReferenceService)) as IReferenceService; if (service != null) { reference = service.GetComponent(context.Instance); } string str = string.Empty; DefaultPropertyAttribute attribute = propDesc.Attributes[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute; if (((attribute != null) && (attribute.Name != null)) && (attribute.Name.Length > 0)) { str = propDesc.Name + "." + attribute.Name; } if (reference != null) { ActivityDesigner designer = ActivityDesigner.GetDesigner(reference as Activity); if (designer != null) { if ((!designer.IsLocked && ActivityBindPropertyDescriptor.IsBindableProperty(propDesc)) && !propDesc.IsReadOnly) { valueUIItemList.Add(new PropertyValueUIItem(DR.GetImage("Bind"), new PropertyValueUIItemInvokeHandler(this.OnBindProperty), DR.GetString("BindProperty", new object[0]))); } string name = service.GetName(reference); string str3 = service.GetName(context.Instance); str3 = (str3.Length > name.Length) ? (str3.Substring(name.Length + 1, (str3.Length - name.Length) - 1) + "." + propDesc.Name) : string.Empty; foreach (DesignerAction action in designer.DesignerActions) { string propertyName = action.PropertyName; if (((propertyName != null) && (propertyName.Length != 0)) && (((propertyName == propDesc.Name) || (propertyName == str3)) || (propertyName == str))) { PropertyValueUIItemHandler handler = new PropertyValueUIItemHandler(action); valueUIItemList.Add(new PropertyValueUIItem(action.Image, new PropertyValueUIItemInvokeHandler(handler.OnFixPropertyError), action.Text)); break; } } } } }
public override string ToString() { if (!isDefaultPropertyAttributeInit) { DefaultPropertyAttribute attrib = XafTypesInfo.Instance.FindTypeInfo(GetType()).FindAttribute <DefaultPropertyAttribute>(); if (attrib != null) { defaultPropertyMemberInfo = ClassInfo.FindMember(attrib.Name); } isDefaultPropertyAttributeInit = true; } if (defaultPropertyMemberInfo != null) { object obj = defaultPropertyMemberInfo.GetValue(this); if (obj != null) { return(obj.ToString()); } } return(base.ToString()); }
// <summary> // Looks up the DefaultPropertyAttribute on the given type and returns the default property, // if any. // </summary> // <param name="type">Type to look up</param> // <returns>Default property associated with the specified type, if any.</returns> public static string GetDefaultProperty(Type type) { if (type == null) { return(null); } string defaultProperty; if (_defaultPropertyCache.TryGetValue(type, out defaultProperty)) { return(defaultProperty); } DefaultPropertyAttribute dpa = GetAttribute <DefaultPropertyAttribute>(type); defaultProperty = dpa == null ? null : dpa.Name; defaultProperty = defaultProperty == null ? null : defaultProperty.Trim(); _defaultPropertyCache[type] = defaultProperty; return(defaultProperty); }
public override PropertyDescriptor GetDefaultProperty() { if (this.Instance != null) { string b = null; PSMemberSet pSStandardMembers = this.Instance.PSStandardMembers; if (pSStandardMembers != null) { PSNoteProperty property = pSStandardMembers.Properties["DefaultDisplayProperty"] as PSNoteProperty; if (property != null) { b = property.Value as string; } } if (b == null) { object[] customAttributes = this.Instance.BaseObject.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute), true); if (customAttributes.Length == 1) { DefaultPropertyAttribute attribute = customAttributes[0] as DefaultPropertyAttribute; if (attribute != null) { b = attribute.Name; } } } PropertyDescriptorCollection properties = this.GetProperties(); if (b != null) { foreach (PropertyDescriptor descriptor in properties) { if (string.Equals(descriptor.Name, b, StringComparison.OrdinalIgnoreCase)) { return(descriptor); } } } } return(null); }
public static IEnumerable <object[]> Equals_TestData() { var attribute = new DefaultPropertyAttribute("name"); yield return(new object[] { attribute, attribute, true }); yield return(new object[] { attribute, new DefaultPropertyAttribute("name"), true }); yield return(new object[] { attribute, new DefaultPropertyAttribute("name2"), false }); yield return(new object[] { attribute, new DefaultPropertyAttribute(null), false }); yield return(new object[] { new DefaultPropertyAttribute(null), new DefaultPropertyAttribute(null), true }); yield return(new object[] { new DefaultPropertyAttribute(null), new DefaultPropertyAttribute("name"), false }); yield return(new object[] { new DefaultPropertyAttribute(null), null, false }); yield return(new object[] { attribute, new object(), false }); yield return(new object[] { attribute, null, false }); }
/// <summary> /// Gets the default property name. /// </summary> /// <param name="type">The type.</param> /// <returns> /// The property name if it exists; otherwise <c>null</c>. /// </returns> public static string GetDefaultPropertyName(Type type) { ArgumentValidator.NotNull(type, "type"); object[] attrs = type.GetCustomAttributes(false); foreach (object attr in attrs) { ContentPropertyAttribute contentAttribute = attr as ContentPropertyAttribute; if (contentAttribute != null) { return(contentAttribute.Name); } DefaultPropertyAttribute defaultAttribute = attr as DefaultPropertyAttribute; if (defaultAttribute != null) { return(defaultAttribute.Name); } } return(null); }
/// <summary> /// <para>Returns a human-readable string that represents the current business object. /// </para> /// </summary> /// <returns>A string representing the current business object. /// </returns> public override string ToString() { if (!BaseObject.IsXpoProfiling) { if (!this.isDefaultPropertyAttributeInit) { string text = string.Empty; XafDefaultPropertyAttribute xafDefaultPropertyAttribute = XafTypesInfo.Instance.FindTypeInfo(base.GetType()).FindAttribute <XafDefaultPropertyAttribute>(); if (xafDefaultPropertyAttribute != null) { text = xafDefaultPropertyAttribute.Name; } else { DefaultPropertyAttribute defaultPropertyAttribute = XafTypesInfo.Instance.FindTypeInfo(base.GetType()).FindAttribute <DefaultPropertyAttribute>(); if (defaultPropertyAttribute != null) { text = defaultPropertyAttribute.Name; } } if (!string.IsNullOrEmpty(text)) { this.defaultPropertyMemberInfo = base.ClassInfo.FindMember(text); } this.isDefaultPropertyAttributeInit = true; } if (this.defaultPropertyMemberInfo != null) { object value = this.defaultPropertyMemberInfo.GetValue(this); if (value != null) { return(value.ToString()); } } } return(base.ToString()); }
public override string ToString() { if (!_isDefaultPropertyAttributeInit) { string defaultPropertyName = string.Empty; XafDefaultPropertyAttribute xafDefaultPropertyAttribute = XafTypesInfo.Instance.FindTypeInfo(GetType()).FindAttribute <XafDefaultPropertyAttribute>(); if (xafDefaultPropertyAttribute != null) { defaultPropertyName = xafDefaultPropertyAttribute.Name; } else { DefaultPropertyAttribute defaultPropertyAttribute = XafTypesInfo.Instance.FindTypeInfo(GetType()).FindAttribute <DefaultPropertyAttribute>(); if (defaultPropertyAttribute != null) { defaultPropertyName = defaultPropertyAttribute.Name; } } if (!string.IsNullOrEmpty(defaultPropertyName)) { _defaultPropertyMemberInfo = GetType().ToTypeInfo().FindMember(defaultPropertyName); } _isDefaultPropertyAttributeInit = true; } if (_defaultPropertyMemberInfo != null) { try { return(_defaultPropertyMemberInfo.GetValue(this)?.ToString()); } catch { // ignored } } return(base.ToString()); }
public override bool Equals(object obj) { DefaultPropertyAttribute other = obj as DefaultPropertyAttribute; return((other != null) && other.Name == Name); }
//private private void CreateEditControl() { TableLayoutPanel layoutPanel = NewRow(); foreach (PropertyInfo p in _properties) { AutoGenControlAttribute a = p.GetAttribute <AutoGenControlAttribute>(); if (a == null) { continue; } //布局分为两块,左边和右边。 //如果左边被占用就放右边,右边也被占用就另起一行。 int colIndex = 0; //左 if (layoutPanel.GetControlFromPosition(colIndex, 0) != null) { //右 前提没有强制另起一行 if (a.BeginNewRow) { layoutPanel = NewRow(); } else { colIndex = 3; if (layoutPanel.GetControlFromPosition(colIndex, 0) != null) { //都占用就用新行的左边 layoutPanel = NewRow(); colIndex = 0; } } } //TODO: 长控件独占一行 // create Editor Control editor = (Control)Activator.CreateInstance(a.EditorType); editor.Name = a.EditorType.Name.LowerFirstLetter() + "_" + p.Name; editor.Dock = DockStyle.Fill; editor.Margin = new Padding(10, 12, 80, 10); editor.Enabled = a.Enabled; //read only var readOnlyProperty = (from pp in a.EditorType.GetProperties() where pp.Name == "ReadOnly" && pp.CanWrite select pp).FirstOrDefault(); if (readOnlyProperty != null) { switch (_formPurpose) { case EditFormPurpose.Create: readOnlyProperty.SetValue(editor, a.ReadOnlyWhenCreate, null); break; case EditFormPurpose.Modify: readOnlyProperty.SetValue(editor, a.ReadOnlyWhenModify, null); break; default: readOnlyProperty.SetValue(editor, true, null); break; } } editor.Parent = layoutPanel; layoutPanel.SetColumn(editor, colIndex + 1); layoutPanel.SetRow(editor, 0); //dataBind DefaultPropertyAttribute @default = editor.GetType().GetAttribute <DefaultPropertyAttribute>(); if (@default != null) { editor.DataBindings.Add(new Binding(@default.Name, _viewModel, p.Name, true, DataSourceUpdateMode.OnPropertyChanged)); } // create Label Label label = new Label { Name = "lable_" + p.Name, AutoSize = false, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleRight, Margin = new Padding(10, 11, 3, 11), Text = a.DisplayName ?? p.Name, Parent = layoutPanel, Tag = editor, }; layoutPanel.SetColumn(label, colIndex); layoutPanel.SetRow(label, 0); label.Click += Label_Click; } }
public void Ctor_Name(string name) { var attribute = new DefaultPropertyAttribute(name); Assert.Equal(name, attribute.Name); }
public void GetHashCode_InvokeMultipleTimes_ReturnsEqual() { var attribute = new DefaultPropertyAttribute("name"); Assert.Equal(attribute.GetHashCode(), attribute.GetHashCode()); }
public void Equals_Other_ReturnsExpected(DefaultPropertyAttribute attribute, object other, bool expected) { Assert.Equal(expected, attribute.Equals(other)); }
public void DesignerAttributes_DefaultPropertyAttribute_PropertyExists(Type type, DefaultPropertyAttribute attribute) { var propertyInfo = type.GetProperty(attribute.Name); _output.WriteLine($"{type.FullName}: {attribute.Name} --> {propertyInfo?.Name}"); Assert.NotNull(propertyInfo); }
public static string GetDefaultPropertyName(object instance) { DefaultPropertyAttribute attribute = (DefaultPropertyAttribute)TypeDescriptor.GetAttributes(instance)[typeof(DefaultPropertyAttribute)]; return(attribute != null ? attribute.Name : (string)null); }