Add Support for Adding .NET Attributes when Creating .NET Classes
See original GitHub issueEnvironment
- 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 * 2
Regarding 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.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Tutorial: Define and read custom attributes.
In this tutorial, you learn how to add attributes to your code, how to create and use your own attributes, and how to...
Read more >Writing Custom Attributes
Design your own custom attributes in .NET. Custom attributes are essentially classes derived directly or indirectly from System.Attribute.
Read more >Implementing Custom Attributes in .NET
A custom attribute is a class that inherits from the System.Attribute class or a subclass of System.Attribute. Custom attributes are created ...
Read more >Working with Classes on a Schematic & PCB in ...
When transferring the design from the schematic to PCB, Altium Designer provides support for the generation of component and net classes.
Read more >Proper Architecture: Adding Attributes to Domain Model in . ...
1 Answer 1 · Metadata Classes. You can create metadata classes and add attributes like data annotations and validation attributes to those ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also: @rmadsen-ks is there anything that is keeping us from just constructing the instances in Python already, like
__clr_attributes__ = [DebuggerDisplayAttribute("ExampleClrClass: X={X}")]
?It could make sense to prefix all of the class members that we use with
clr
, like__clr_attributes__
.