question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Support for Adding .NET Attributes when Creating .NET Classes

See original GitHub issue

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:

  1. Attributes on types - This can be done with an annotation
  2. Attributes on methods - This can be done with an annotation
  3. 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:open
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
filmorcommented, Apr 21, 2022

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}")]?

1reaction
filmorcommented, Apr 21, 2022

It could make sense to prefix all of the class members that we use with clr, like __clr_attributes__.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found