Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

how to get ironpython method parameters using .net

$
0
0

here is my python code:

#filename:animal.py
__metaclass__=type
class Animal():
    def __init__(self):
        self.id=-1
        self.name=''
    def doSomething(self):
        print 'animal...'
    def toString(self):
        print 'id={0},Name={1}'.format(self.id,self.name)

class Cat(Animal):
    def __init__(self):
        #super(Animal,self).__init__()
        Animal.__init__(self)
        
    def doSomething(self):
        print 'this is {0}...'.format(self.name)

print 'hello'
if __name__=='__main__':
    a=Animal()
    print a.id,a.name
#filename:animalset.py
__metaclass__=type
from animal import Animal,Cat
class AnimalSet():
    def __init__(self):
       a0=Animal()
       a0.id,a0.name=1,'unknown'
       a1=Cat()
       a1.id,a1.name=2,'Tom'
       a2=Cat()
       a2.id,a2.name=3,'Paul'
       self.arr=[a0,a1,a2]

    def set(self):
        return self.arr

    def add(self,animal):
        self.arr.append(animal)

    def find(self,index):
        for i,value in enumerate(self.arr):
            if i==index:
                return value
       

generally,we can invoke ironpython class and its members using the .net library ironpyton.dll,

here is my sample:

           ScriptEngine engine = Python.CreateEngine();
            string ironpath = @"D:\ironpythondemo\ipylib";
            engine.SetSearchPaths(new string[] { ironpath });

            ScriptSource source = engine.CreateScriptSourceFromFile(Path.Combine(ironpath, "animset.py"));
            ScriptScope scope = engine.CreateScope();
            ObjectOperations ops = engine.CreateOperations();
            source.Execute(scope);
 	    dynamic animalSet = scope.GetVariable("AnimalSet");
            dynamic animal = animalSet().find(0);
            Console.WriteLine("id:{0}", animal.id);
            Console.WriteLine("name:{0}", animal.name);

            Console.ReadKey();


it is likely to invoke ironpython members using ObjectOperations and dlr(dynamic keywords), but i want to know more about ironpython class members such as how to get python method parameters.

we can use .net reflection to get parameters from .net class easily:

      var cls = typeof(System.IO.FileStream);
            var method = cls.GetMethod("Read");
            ParameterInfo[] parameters = method.GetParameters();

So, how can i get python method paramters using .net?

for example,in the file “animalset.py” ,the “AnimalSet“ class has the method ”find“,then how to get parameters from method 'find'?




Viewing all articles
Browse latest Browse all 31927

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>