2using System.Collections.Generic;
22 this.Converters =
new List<PyClrTypeBase>();
23 this.PythonConverters =
new Dictionary<IntPtr, Dictionary<Type, PyClrTypeBase>>();
24 this.ClrConverters =
new Dictionary<Type, Dictionary<IntPtr, PyClrTypeBase>>();
27 private List<PyClrTypeBase> Converters;
29 private Dictionary<IntPtr, Dictionary<Type, PyClrTypeBase>> PythonConverters;
31 private Dictionary<Type, Dictionary<IntPtr, PyClrTypeBase>> ClrConverters;
39 this.Converters.Add(converter);
41 Dictionary<Type, PyClrTypeBase> py_converters;
42 var state = this.PythonConverters.TryGetValue(converter.
PythonType.Handle, out py_converters);
45 py_converters =
new Dictionary<Type, PyClrTypeBase>();
46 this.PythonConverters.Add(converter.
PythonType.Handle, py_converters);
48 py_converters.Add(converter.
ClrType, converter);
50 Dictionary<IntPtr, PyClrTypeBase> clr_converters;
51 state = this.ClrConverters.TryGetValue(converter.
ClrType, out clr_converters);
52 if (!this.ClrConverters.ContainsKey(converter.
ClrType))
54 clr_converters =
new Dictionary<IntPtr, PyClrTypeBase>();
55 this.ClrConverters.Add(converter.
ClrType, clr_converters);
57 clr_converters.Add(converter.
PythonType.Handle, converter);
68 if (converter ==
null)
81 this.AddListType<object>(converter);
91 if (converter ==
null)
106 if (converter ==
null)
121 return (T)
ToClr(obj, typeof(T));
130 public object ToClr(PyObject obj, Type t =
null)
136 PyObject type = obj.GetPythonType();
137 Dictionary<Type, PyClrTypeBase> converters;
138 var state = PythonConverters.TryGetValue(type.Handle, out converters);
143 if (t ==
null || !converters.ContainsKey(t))
145 return converters.Values.First().ToClr(obj);
149 return converters[t].ToClr(obj);
160 public PyObject
ToPython(
object clrObj, IntPtr? t =
null)
166 Type type = clrObj.GetType();
167 Dictionary<IntPtr, PyClrTypeBase> converters;
168 var state = ClrConverters.TryGetValue(type, out converters);
171 throw new Exception($
"Type {type.ToString()} not recognized");
173 if (t ==
null || !converters.ContainsKey(t.Value))
175 return converters.Values.First().ToPython(clrObj);
179 return converters[t.Value].ToPython(clrObj);
238 public abstract object ToClr(PyObject pyObj);
265 Func<PyObject, object> py2clr, Func<object, PyObject> clr2py)
266 : base(pyType, clrType)
268 this.Py2Clr = py2clr;
269 this.Clr2Py = clr2py;
275 private Func<PyObject, object> Py2Clr;
280 private Func<object, PyObject> Clr2Py;
287 public override object ToClr(PyObject pyObj)
289 return this.Py2Clr(pyObj);
299 return this.Clr2Py(clrObj);
316 : base(
"str", typeof(string))
325 public override object ToClr(PyObject pyObj)
327 return pyObj.As<
string>();
337 return new PyString(Convert.ToString(clrObj));
354 : base(
"bool", typeof(bool))
363 public override object ToClr(PyObject pyObj)
365 return pyObj.As<
bool>();
376 throw new NotImplementedException();
393 : base(
"int", typeof(int))
402 public override object ToClr(PyObject pyObj)
404 return pyObj.As<
int>();
414 return new PyInt(Convert.ToInt32(clrObj));
431 : base(
"int", typeof(long))
440 public override object ToClr(PyObject pyObj)
442 return pyObj.As<
long>();
452 return new PyInt(Convert.ToInt64(clrObj));
469 : base(
"float", typeof(float))
478 public override object ToClr(PyObject pyObj)
480 return pyObj.As<
float>();
490 return new PyFloat(Convert.ToSingle(clrObj));
507 : base(
"float", typeof(double))
516 public override object ToClr(PyObject pyObj)
518 return pyObj.As<
double>();
528 return new PyFloat(Convert.ToDouble(clrObj));
656 this.
ClrType = info.PropertyType;
682 var clr_value = this.
PropertyInfo.GetValue(clrObj,
null);
741 var clr_value = this.FieldInfo.GetValue(clrObj);
755 this.FieldInfo.SetValue(clrObj, clr_value);
774 : base(pyType, typeof(T))
776 this.Converter = converter;
777 this.Properties =
new List<ClrMemberInfo>();
780 foreach (var property
in this.
ClrType.GetProperties())
783 if (attr.Length == 0)
788 this.Properties.Add(
new ClrPropertyInfo(property, py_info, this.Converter));
791 foreach (var field
in this.
ClrType.GetFields())
794 if (attr.Length == 0)
799 this.Properties.Add(
new ClrFieldInfo(field, py_info, this.Converter));
811 private List<ClrMemberInfo> Properties;
818 public override object ToClr(PyObject pyObj)
820 var clrObj = Activator.CreateInstance(this.
ClrType);
821 foreach (var item
in this.Properties)
823 item.SetClrObjAttr(clrObj, pyObj);
836 foreach (var item
in this.Properties)
838 item.SetPyObjAttr(pyObj, clrObj);
859 : base(
"list", typeof(List<T>))
861 this.Converter = converter;
874 public override object ToClr(PyObject pyObj)
876 var dict = this._ToClr(
new PyList(pyObj));
885 private object _ToClr(PyList pyList)
887 var list =
new List<T>();
888 foreach (PyObject item
in pyList)
890 var _item = this.Converter.
ToClr<T>(item);
903 return this.
_ToPython(clrObj as List<T>);
913 var pyList =
new PyList();
914 foreach (var item
in clrObj)
916 PyObject _item = this.Converter.
ToPython(item);
917 pyList.Append(_item);
939 : base(
"dict", typeof(Dictionary<K, V>))
941 this.Converter = converter;
954 public override object ToClr(PyObject pyObj)
956 var dict = this._ToClr(
new PyDict(pyObj));
965 private object _ToClr(PyDict pyDict)
967 var dict =
new Dictionary<K, V>();
968 foreach (PyObject key
in pyDict.Keys())
970 var _key = this.Converter.
ToClr<K>(key);
971 PyObject objVal = pyDict[key];
973 var _value = this.Converter.
ToClr<V>(objVal);
974 dict.Add(_key, _value);
986 return this.
_ToPython(clrObj as Dictionary<K, V>);
996 var pyDict =
new PyDict();
997 foreach (var item
in clrObj)
999 PyObject _key = this.Converter.
ToPython(item.Key);
1000 PyObject _value = this.Converter.
ToPython(item.Value);
1001 pyDict[_key] = _value;
The BooleanType represents a clr bool type.
BooleanType()
The constructor.
override PyObject ToPython(object clrObj)
Converts a bool to a PyObject.
override object ToClr(PyObject pyObj)
Converts a PyObject to a bool.
The ClrFieldInfo defines the clr field information.
override void SetPyObjAttr(PyObject pyObj, object clrObj)
Sets the Python object attribute.
ClrFieldInfo(FieldInfo info, PyPropetryAttribute py_info, PyConverter converter)
The constructor.
FieldInfo FieldInfo
Returns the field information.
override void SetClrObjAttr(object clrObj, PyObject pyObj)
Sets the clr object attribute.
The ClrMemberInfo represents clr information.
abstract void SetPyObjAttr(PyObject pyObj, object clrObj)
Set the Python object attribute.
Type ClrType
Returns the clr type.
string ClrPropertyName
Returns the clr property name.
PyConverter Converter
Returns the converter used.
abstract void SetClrObjAttr(object clrObj, PyObject pyObj)
Set the clr object attribute.
IntPtr? PythonType
Returns the Python type.
string PyPropertyName
Returns the Python property name.
The ClrPropertyInfo specifies the clr property information.
override void SetClrObjAttr(object clrObj, PyObject pyObj)
Sets the clr object attribute.
override void SetPyObjAttr(PyObject pyObj, object clrObj)
Sets the Python object attribute.
ClrPropertyInfo(PropertyInfo info, PyPropetryAttribute py_info, PyConverter converter)
The constructor.
PropertyInfo PropertyInfo
Return the clr Property information.
The DoubleType represents a clr double type.
DoubleType()
The constructor.
override PyObject ToPython(object clrObj)
Converts a double to a PyObject.
override object ToClr(PyObject pyObj)
Converts a PyObject to a float.
The FloatType represents a clr float type.
FloatType()
The constructor.
override object ToClr(PyObject pyObj)
Converts a PyObject to a float.
override PyObject ToPython(object clrObj)
Converts a float to a PyObject.
The Int32Type represents a clr int type.
Int32Type()
The constructor.
override PyObject ToPython(object clrObj)
Converts a int to a PyObject.
override object ToClr(PyObject pyObj)
Converts a PyObject to a int.
The Int64Type represents a clr long type.
override PyObject ToPython(object clrObj)
Converts a long to a PyObject.
Int64Type()
The constructor.
override object ToClr(PyObject pyObj)
Converts a PyObject to a long.
Convert between Python object and clr object
override object ToClr(PyObject pyObj)
Converts the PyObject to a clr object.
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
ObjectType(PyObject pyType, PyConverter converter)
The constructor.
The PyClrTypeBase is the base class for other types.
PyClrTypeBase(PyObject pyType, Type clrType)
The constructor.
PyClrTypeBase(string pyType, Type clrType)
The constructor.
abstract PyObject ToPython(object clrObj)
Converts the clr object to a PyObject type.
abstract object ToClr(PyObject pyObj)
Converts the PyObject type to the clr object.
Type ClrType
Returns the clr type.
PyObject PythonType
Returns the Python type.
The PyClrType class defines a Python clr type.
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
PyClrType(PyObject pyType, Type clrType, Func< PyObject, object > py2clr, Func< object, PyObject > clr2py)
The constructor.
override object ToClr(PyObject pyObj)
Converts a PyObject to a clr object.
use PyConverter to convert between python object and clr object.
PyObject ToPython(object clrObj, IntPtr? t=null)
Convert a clr object to a PyObject.
void AddListType(PyConverter converter=null)
Add a new list type to the converter.
void AddObjectType< T >(PyObject pyType, PyConverter converter=null)
Add a new PyObjec type to the converter.
void AddDictType< K, V >(PyConverter converter=null)
Add a new dictionary type of K key types and V value types.
void Add(PyClrTypeBase converter)
Add a new type for conversion.
object ToClr(PyObject obj, Type t=null)
Convert a PyObject to a clr type of 't'.
T ToClr< T >(PyObject obj)
Convert the PyObject to a specifict type T
void AddListType< T >(PyConverter converter=null)
Add a new list type to the converter.
Specifies a PyDictionType dictionary.
override object ToClr(PyObject pyObj)
Convert a PyObject to a clr object.
override PyObject ToPython(object clrObj)
Convert a clr object to a PyObjec.t
PyObject _ToPython(Dictionary< K, V > clrObj)
Convert a clr Dictionary to a PyDict and return it as a PyObject.
PyDictType(PyConverter converter)
The constructor.
Defines a PyListType of type 'T'
PyListType(PyConverter converter)
The constructor.
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
override object ToClr(PyObject pyObj)
Converts the PyObject to a clr object.
PyObject _ToPython(List< T > clrObj)
Converts a clr List to a PyList.
The PyPropertyAttribute represents a Python attribute.
string Name
Returns the attribute name.
PyPropetryAttribute(string name, string py_type=null)
The constructor.
PyPropetryAttribute()
The constructor.
string PythonTypeName
Returns the atttribute type name.
IntPtr? PythonType
Returns the Python type.
The StringType represents a clr string type.
override object ToClr(PyObject pyObj)
Converts a PyObject to a string.
override PyObject ToPython(object clrObj)
Converts a string to a PyObject.
StringType()
The constructor.