0

Interfaces are no types, so when implementing them, their property attributes are not inherited. (which would be true of a base-class)

So, I'm currently wrapping my head around how to make this happen. Following example should pretty much explain what I mean, but unfortunately is not working as expected.

Reason is obvisously to centralize Instructions / Attributes without having to copy / paste them over and over.

public interface IPermissionBasedControl
 {
     [Browsable(true), Category("PermissionSystem"), Description("AccessKey required in order to use this control.")]
     String PermissionKey { get; set; }
 }

 public class ExtendedTextBox : TextBox, IReadOnlyControl, IDataBindable, IPermissionBasedControl
 {
    ...
    [AttributeProvider(typeof(IPermissionBasedControl))]
    public string PermissionKey { get; set; }
    ...
 }

Expectation: Visual Studio Designer will pick the Category, Browsable and Description Attribute from IPermissionBasedControl and display the property PermissionKey accordingly in the Designers Property-Window, when working with a ExtendedTextBox.

Actual result: Nothing happens, Default Category Misc is applied, no Description, Browsable default yes is applied.

  • Is it not working, because Visual Studio Designer does not respect the AttributProvider-Attribute?
  • Is it not working, because AttributeProvider cannot target an Interface?

I've also tried method access to no success:

 public class ExtendedTextBox : TextBox, IReadOnlyControl, IDataBindable, IPermissionBasedControl
 {
    ...
    [AttributeProvider(nameof(IPermissionBasedControl), nameof(IPermissionBasedControl.PermissionKey))]
    public string PermissionKey { get; set; }
    ...
 }
dognose
  • 20,360
  • 9
  • 61
  • 107
  • 1
    Imagine that you had an attribute with `AllowMultiple=false`. Then you create two interfaces, each of which define a property of the same name, and each puts this attribute on it. Then create a class which implements these two interfaces, with a single property. How would that work? If the attributes were transferred to the property, then you'd have inherited two instances of an attribute which has `AllowMultiple=false` – canton7 Feb 01 '23 at 16:46
  • In that Case I would still have the choice, which Interfaces Attributes I reference with `AttributeProvider` (Or Do I missunderstand the principle of `AttributeProvider`?) – dognose Feb 01 '23 at 16:48
  • Hello, your thinking is very ingenious. I found a thread on the platform, hope it can help you. https://stackoverflow.com/questions/59080470/c-sharp-interface-implementation-with-an-interface-property – wenbingeng-MSFT Feb 03 '23 at 02:32

1 Answers1

0

Okay, It seems like I had a wrong interpretation about what AttributeProvider is doing:

AttributeProviders description is Enables Attribute Redirection - but this doesn't mean, that it will (in my example) attach the Attributes of IPermissionBasedControl.PermissionKey to ExtendedTextBox.PermissionKey, the AttributeProvider is having effect on the Attributes of the type of the property.

i.e.: If I would specify

    [AttributeProvider(typeof(Color))]
    public object PermissionKey { get; set; }

the VisualStudio Designer would now Apply the Attributes of Color to the Value-Field for picking the object. (And therefore show a color-picker)

So, technically spoken, the AttributeProvider redirects Attributes for the type of the property, not for the property itself.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • Thank you for your efforts. After reading your answer, it's like clearing away the clouds and mist. You can mark this as an answer to help more people. – wenbingeng-MSFT Feb 09 '23 at 01:47