-
Notifications
You must be signed in to change notification settings - Fork 770
Open
Description
Environment
- Pythonnet version: 3.x
- Python version: 3.x
- Operating System: Any
- .NET Runtime: .Net Framework 4 / .Net6
Details
I want to be able to specify attributes on my class defined in python. This is essential for my specific use case, but I imagine generally useful. I can come up with 3 different situations where it could be useful to add attributes:
- Attributes on types - This can be done with an annotation
- Attributes on methods - This can be done with an annotation
- Attributes on properties - It seems clr.clrproperty needs to be modified to support it.
### from test_clrmethod.py
# Attaching attributes to types (1)
@clr.attribute(DebuggerDisplayAttribute, "ExampleClrClass: X={X}")
class ExampleClrClass(System.Object):
__namespace__ = "PyTest"
def __init__(self):
self._x = 3
# Attaching attributes to methods (2)
@clr.clrmethod(int, [int])
@clr.attribute(DescriptionAttribute, "Test method")
def test(self, x):
return x*2
def get_X(self):
return self._x
def set_X(self, value):
self._x = value
# Attaching attributes to properties (3)
X = clr.clrproperty(int, get_X, set_X)\
.attribute(DescriptionAttribute, "Gets or sets X")\
.attribute(BrowsableAttribute, True)
# Also attaching attributes to properties (3)
@clr.clrproperty(int)
@clr.attribute(DescriptionAttribute, "Gets Y")
def Y(self):
return self._x * 2Regarding using clr.attribte(DebuggerDisplayAttribute, "...") vs clr.attribute(DebuggerDisplay("...")), it is exceedingly hard to infer how an attribute is constructed form the attribute value itself. Instead the list of constructors can be iterated and matched with a list of arguments and this way the best matching constructor can be found.
I can probably implement this myself, but before making a PR I want to know if it will be accepted or it is a bad idea.
Metadata
Metadata
Assignees
Labels
No labels