Dynamically invoking the IronPython script in a C # environment (II)

Source: Internet
Author: User
Tags error handling

One, the Python data type corresponds to C # data type

Simple types in data types in Python, such as int,float,string, can correspond to int32,double,string in the C # environment, which are intuitive, complex data types in Python, such as List,set, are C # Not in the environment, fortunately IronPython provides the C # interface for these data types so that we can use them in the C # environment. As shown in the following table.

C # Python

Ironpython.runtime.setcollection―――set

Ironpython.runtime.list――――list

Ironpython.runtime.pythondictionary――dictionary

(The data types listed in this article are sufficient to be used as a study to list the correspondence of other data types, but the actual programming is not necessary.) )

Is the inheritance diagram for setcollection, list, and pythondictionary.

The author thinks that the function of these interface classes is to "communicate" with Python, to be able to remove or deposit data from these packages, the post-processing or pre-processing of these data should be used in C # environment "native class" is more appropriate, rather than some of the special methods of these encapsulation classes. So here are some of the basic methods of these encapsulation classes (primarily access data).

[CSharp]View PlainCopy
  1. Setcollection Main methods
  2. Setcollection copy ()
  3. void Clear ()
  4. ienumerator< object > GetEnumerator ()
  5. int Count
  6. IronPython.Runtime.List Main methods
  7. int index (object Item)
  8. int index (object item, int start)
  9. int index (object item, int start, int stop)
  10. void Insert (int index, object value)
  11. void Insert (int index, object value)
  12. Object Pop ()
  13. Object Pop (int index)
  14. void Remove (object value)
  15. void reverse ()
  16. void RemoveAt (int index)
  17. BOOL Contains (object value)
  18. void Clear ()
  19. int IndexOf (object value)
  20. int Add (object value)
  21. void CopyTo (array array, int index)
  22. void CopyTo (object[] array, int arrayindex)
  23. BOOL Remove (object Item)
  24. int Count
  25. Pythondictionary Main methods
  26. void Add (object key, object value)
  27. BOOL ContainsKey (object key)
  28. BOOL Remove (object key)
  29. BOOL TryGetValue (object key, out object value)
  30. void Add (keyvaluepair< object, object > Item)
  31. void Clear ()
  32. BOOL Contains (keyvaluepair< object, object > Item)
  33. void CopyTo (keyvaluepair< object, object >[] array, int arrayindex)
  34. int Count

These methods are similar to the set classes inherent in C # and are useful.

Second, script error handling

The running of dynamic scripts, because of the components of user participation, so the likelihood of error is very large, script parsing and running, should be included in a large try ... Catch, the application should not be interrupted because of the error of the script, can give a friendly, meaningful error message, this kind of program must consider the problem.

1. If scripting errors (syntax errors) occur, SyntaxErrorException is generated when executing execute on the script.

2. In the script, if no class or method is found, generates Unboundnameexception, no method is defined in the class, produces MissingMemberException, the method passes in the wrong number of arguments, produces argumenttypeexception, Incoming parameter type is incorrect, resulting in typeerrorexception

3. If the script is running correctly, when getvariable, the variable name is incorrectly written, resulting in missingmemberexception.

4. The method or class unboundnameexception is not found in the script, and method missingmemberexception is not defined in the class.

The above lists only script syntax errors and call errors, does not contain run errors (for example, is removed by 0), and more detailed exception information, please consult IronPython's help documentation.

Dynamically invoking the IronPython script in a C # environment (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.