Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [AttributeUsage(AttributeTargets.Property)]
- class TextProperyAttribute: Attribute
- {}
- class MyTextBox
- {
- [TextPropery]
- public string Text { get; set;}
- public int Foo { get; set;}
- public double Bar { get; set;}
- }
- static string GetTextProperty(Type type)
- {
- foreach (PropertyInfo info in type.GetProperties())
- {
- if (info.GetCustomAttributes(typeof(TextProperyAttribute), true).Length > 0)
- {
- return info.Name;
- }
- }
- return null;
- }
- ...
- Type type = typeof (MyTextBox);
- string name = GetTextProperty(type);
- Console.WriteLine(name); // Prints "Text"
- private string GetPropertyName<TValue>(Expression<Func<BindingSourceType, TValue>> propertySelector)
- {
- var memberExpression = propertySelector.Body as MemberExpression;
- return memberExpression != null
- ? memberExpression.Member.Name
- : string.empty;
- }
- this.textBox.DataBindings.Add(GetPropertyName(o => o.MyClassProperty),
- this.myDataSourceObject,
- "Text");
Add Comment
Please, Sign In to add comment