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.

Classes without __namespace__ cannot be used for reflection

See original GitHub issue

Environment

  • Pythonnet version: 3.0.0-preview2022-04-11
  • Python version: 3.10
  • Operating System: MacOS
  • .NET Runtime: .net 6

Details

My software does lots of reflection on “plugin” types. Simply inheriting from a specific type is enough in C#, but when I use python.net I also have to define __namespace__. Since my users generally have the namespace being <Module>.<Class>, there is no logic reason for them to have to do this.

I can see which part of the code causes this:

    // from MetaType.cs:133 (git: 7247da55c174be1b733e5f9fc4e1c356f6c42dc4)
    if (clsDict.HasKey("__assembly__") || clsDict.HasKey("__namespace__"))
    {
            return TypeManager.CreateSubType(name, base_type, clsDict);
    }

I am wonder if we could broaden the cases where TypeManager.CreateSubType is used. For example in cases where the super class is a type created with CreateSubType, or just any .NET class in general. Maybe it could also be if the super class implements a specific interface.

_TODO_

Add a way to not have to specify namespace in the class in order for reflection to work.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
rmadsen-kscommented, Nov 18, 2022

Hi @filmor, sure, but I have a bunch of features on that fork, some more or less experimental. In total, it is not that many changes. I’ll clean it up a bit and setup a PR.

0reactions
filmorcommented, Nov 29, 2022

@lostmsu I lined out how we could do it anyhow, via a runtime/startup feature flag mechanism. No magic required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# - Is there a way to load classes from a namespace ...
So I tried to use reflection with the code below: ... the bottom line is you can't get around listing all your commands...
Read more >
System.Reflection Namespace
Represents a missing Object. This class cannot be inherited. Performs reflection on a module. Represents nullability information.
Read more >
c# - Get nested type name without namespace
As you've noticed that returns Baz when you want Foo.Bar.Baz . Either you use reflection to get the three types and concatenate their...
Read more >
Get Full Name of Namespace, Can't Use nameof : r/csharp
I am doing reflection on an assembly & am trying to appropriately get the name of a namespace. If I use: string namespace...
Read more >
Manual: Namespaces
A namespace is simply a collection of classes that are referred to using a ... Unity gives the following warning in the console:...
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