Apply attribute class to Python class which Inherits a C# base Class
I have a base class in C# which is like so
Class A
{
public ClassA()
{
}
.....
}
In python I Inherit this base class
import clr
clr.AddReferenceToFile("MyClass.dll")
from MyNamespace import ClassA
class MySubclass(ClassA):
def _init_(self):
print "I'm in a subclass in pyt开发者_如何学Chon"
I also have an attribute class in C# called MyAttrib("Value")
Somewhere later on in my C# code I check that any classes derived from ClassA
have the Attribute MyAttrib
applied to it, which fails in the case of the Python class.
Is there a way to do either of the following :
- Apply an attrib class to the derived class in Python?
- Set the AttributeValue in Python somehow when the object is being instantiated?
- Some other, way to do what I am thinking about
I am using IronPython with plug-ins for Visual studio.
I don't think there's a simple way to do that. It's definitely easier to use a C# stub class having the necessary attributes defined and subclass that.
But, if you really need to define a class like that in python, you can use a workaround (as shown here )
Basically you can compile (in memory) a piece of C# code and import that in IronPython. Given that is a C# piece of code you can add attributes and hence your problem is solved.
This is of course quite convoluted because your basically embedding a language into another making a chain like this: C# <- IronPython <- C#
, then, again, I'd strongly suggest you to define a C# class already having the necessary attributes and use that.
精彩评论