FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Sypport typeof(T) or clr.GetClrType · Issue #432 · pythonnet/pythonnet · GitHub

Repository navigation

Sypport typeof(T) or clr.GetClrType #432

Closed

Description

In order to do reflection, you need to acquire the Type instance for imported class.

In C#, this is done by

Type t = typeof(MyClass)

In IronPython, this is done by

t = clr.GetClrType(MyClass)

Python.net seems to be missing this functionality.

I currently have this horrible hack, and I'm unsure how far it will work:

def get_clr_type(typ):
    """ simulate typeof(T) or GetClrType from IronPython.

    Horrible hack, redo
    """
    name = typ.__module__ + "." + typ.__name__
    return Type.GetType(name)

For reference, code using this resides in: https://github.com/vivainio/pynetext/blob/master/pynetext.py

Activity

  1. den-run-ai commented on Mar 22, 2017

    Contributor

    @vivainio pull requests are welcome!

    Your approach is in the right direction:

    https://mail.python.org/pipermail/pythondotnet/2005-July/000371.html

  2. vivainio commented on Mar 22, 2017

    ContributorAuthor

    @denfromufa Ok, I'll take a look if I can figure it out how to build & work with the codebase :)

  3. den-run-ai commented on Mar 22, 2017

    Contributor

    @vivainio pythonnet runs on 2 CI servers: Travis and Appveyor. So the pull request will automatically do the builds and also run the tests.

    You can also look at these docs to see how to build locally:
    https://github.com/pythonnet/pythonnet/wiki/Troubleshooting-on-Windows-and-Linux

    Please report any issues back about building locally, so that we can improve the wiki.

  4. den-run-ai commented on Mar 26, 2017

    Contributor

    @vivainio thanks for this contribution!

  5. added a commit that references this issue on Jun 1, 2017
  6. KEBO80 commented on Nov 7, 2017

    As an anlternative solution, I came up with the following by creating a static C# helper function:

    public static class ReflectionHelper
    {
        public static Type GetType<T>() => typeof(T);
    }
    

    This I can import this in Python code and use it for any NET type in the folllowing way:

    int32type = ReflectionHelper.GetType[System.Int32]()
    

    intt32type is of System.RuntimeType.

  7. JoaBjo commented on Dec 14, 2017

    Could you elaborate a little bit on your alternative solution @KEBO80? That class doesn't compile since T is not defined.

  8. filmor commented on Dec 15, 2017

    Member

    @JoaBjo Did you try to compile it? :)

    This code is valid, the function GetType<T>() is a generic function using one type parameter T and no function parameters. In C# you would call it as var int32Type = ReflectionHelper.GetType<System.Int32>(), KEBO80 gave the corresponding Python syntax (square brackets [] instead of <> to select the type parameter).

  9. JoaBjo commented on Dec 15, 2017

    Thanks for the reply @filmor. I was confused for a couple of reasons.

    1. GetType was not defined as a generic method and hence it didn't compile.
    2. The square bracket part was also not there in the python code.

    Also for my use case I need the method for a static class. And GetType() doesn't compile for static types in C#. However, for some reason it still works in python when writing GetTypeT for a static type T. I did not expect that.

    Anyways, thanks for clarifying things

  10. den-run-ai commented on Jan 28, 2018

    Contributor

    Here is one corner case that was fixed, but needs review:

    #606

    Basically I scanned through all built-in types in System.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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


      Back | FazBrowse Home | New Git URL