Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
@rmadsen-ks

Description

@rmadsen-ks

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}")classExampleClrClass(System.Object):__namespace__="PyTest"def__init__(self):self._x=3# Attaching attributes to methods (2)@clr.clrmethod(int, [int])@clr.attribute(DescriptionAttribute,"Test method")deftest(self,x):returnx*2defget_X(self):returnself._xdefset_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")defY(self):returnself._x*2

Regarding usingclr.attribte(DebuggerDisplayAttribute, "...") vsclr.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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp