[Ironpython-users] How can IronPython access a public static value defined in C#?

Jimmy Schementi jschementi at gmail.com
Mon Jun 20 18:35:50 CEST 2011

On Jun 20, 2011, at 11:57 AM, Jeff Hardy wrote:
> On Mon, Jun 20, 2011 at 5:16 AM, Doug Blank <doug.blank at gmail.com> wrote:
>> Yes, indeed. So, my question is: how to get a "public static property"
>> to be dynamically included with "from x import *"? You can't mark it
>> readonly, right? And it doesn't seem possible without enumerating the
>> static items myself and creating __all__. Perhaps a special DLR
>> method/setting could be added?
> Could you open an issue on CodePlex and attach a self-contained
> reproduction (the .cs, and a .py file showing what you expect to
> happen)? It could very well be a bug, or a decision that needs to be
> revisited.

Importing static readonly fields, static methods, and events are supported, while static properties are not (see https://github.com/IronLanguages/main/blob/master/Languages/IronPython/IronPython/Runtime/Importer.cs#L798). A static class's field, methods, and events map nicely onto Python's module variables and module functions. What would static properties map to on a Python module? Python new-style classes have properties, but I don't know of a Python construct on modules that would make to a property.

Help me understand your expectations, when would you expect get_SomeStaticProperty to be invoked?

    // C#
    class x {
      static int SomeStaticProperty { get { return new System.Random().Next(); } }
    # property import (doesn't work today)
    from x import SomeStaticProperty 
    print SomeStaticProperty
    print SomeStaticProperty

If we chose to map .NET property imports to module variables, then we'd have to execute the getter when imported, which would break this example (random numbers). We could do some magic of exposing the PropertyInfo itself as the variable, making an getter call look like:


... and a setter call look like:


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110620/eac532e5/attachment-0001.html>

More information about the Ironpython-users mailing list