IronPython에서이를 수행 할 수 있습니까? 그렇다면 어떻게 될까요?IronPython 클래스를 serializable로 표시하면
3
A
답변
4
IronPython 개발자 중 한 명이이 예제를 제공하여 Python 클래스 (예 : Serializable)에 .NET 특성을 추가했습니다.
http://ironpython.codeplex.com/releases/view/36280#DownloadId=116513
2
파이썬의 직렬화 버전은 산세입니다 (믿습니다). 클래스를 직렬화 가능으로 표시해야한다고 생각하지 않습니다. IronPython.Modules에서
는 "(PythonPickle
자위대저장 =의 PythonPickle.dumps을 가져
수입 CLR clr.AddReference ("IronPython.Modules ") :
그래서 당신은이 작업을 수행해야 자위대 "
글쎄, 당신은 defini 수 있습니다") 는 PythonPickle.loads이 (저장) #recovered =이 회복 "
from System.IO import File
from System.Runtime.Serialization.Formatters.Binary import BinaryFormatter #whew
from System.Runtime.Serialization import ISerializable, SerializationException
from System import SerializableAttribute, ObsoleteAttribute
from System.Reflection.Emit import OpCodes, CustomAttributeBuilder
from System.Security.Permissions import *
import clr
clr.AddReference("Microsoft.Dynamic")
clr.AddReference("IronPython")
from Microsoft.Scripting.Generation import Snippets
class ClrTypeMetaclass(type):
def __clrtype__(cls):
baseType = super(ClrTypeMetaclass, cls).__clrtype__()
typename = cls._clrnamespace + "." + cls.__name__ \
if hasattr(cls, "_clrnamespace") \
else cls.__name__
typegen = Snippets.Shared.DefineType(typename, baseType, True, False)
typebld = typegen.TypeBuilder
for ctor in baseType.GetConstructors():
ctorparams = ctor.GetParameters()
ctorbld = typebld.DefineConstructor(
ctor.Attributes,
ctor.CallingConvention,
tuple([p.ParameterType for p in ctorparams]))
ilgen = ctorbld.GetILGenerator()
ilgen.Emit(OpCodes.Ldarg, 0)
for index in range(len(ctorparams)):
ilgen.Emit(OpCodes.Ldarg, index + 1)
ilgen.Emit(OpCodes.Call, ctor)
ilgen.Emit(OpCodes.Ret)
if hasattr(cls, '_clrclassattribs'):
for cab in cls._clrclassattribs:
typebld.SetCustomAttribute(cab)
return typebld.CreateType()
def make_cab(attrib_type, *args, **kwds):
clrtype = clr.GetClrType(attrib_type)
argtypes = tuple(map(lambda x:clr.GetClrType(type(x)), args))
ci = clrtype.GetConstructor(argtypes)
props = ([],[])
fields = ([],[])
for kwd in kwds:
pi = clrtype.GetProperty(kwd)
if pi is not None:
props[0].append(pi)
props[1].append(kwds[kwd])
else:
fi = clrtype.GetField(kwd)
if fi is not None:
fields[0].append(fi)
fields[1].append(kwds[kwd])
else:
raise Exception, "No %s Member found on %s" % (kwd, clrtype.Name)
return CustomAttributeBuilder(ci, args,
tuple(props[0]), tuple(props[1]),
tuple(fields[0]), tuple(fields[1]))
def cab_builder(attrib_type):
return lambda *args, **kwds:make_cab(attrib_type, *args, **kwds)
Serializable = cab_builder(SerializableAttribute)
class Applesauce(ISerializable):
__metaclass__ = ClrTypeMetaclass
_clrnamespace = "Yummy.Yum.Food"
_clrclassattribs = [Serializable()]
def __init__(self):
self.sweetness = 10
def GetObjectData(self,info,context):
info.AddValue("sweetness",10)
binformatter = BinaryFormatter()
output = File.Create("applesauce.dat")
binformatter.Serialize(output,Applesauce())
output.Close()
: tely 내가
here
로 보면서 나는 경우 BinaryFormatter가 걸릴 직렬화하지만, 직렬화 복원이 불가능 할 수 있도록 직렬화 정렬의 그것을 만들 수 있었다 ... 직렬화 하나 표시
물론 출력 파일에는 GetObjectData 메서드에 info.AddValue (...) 형식으로 들어 있기 때문에 "단맛"속성 만 직렬화되었습니다.
그래서 지금 당장은 불가능하다고 결론 내릴 수 있다고 생각합니다. 순수 IronPython에서 직렬화 가능으로 표시하십시오.
관련 문제
- 1. 클래스를 Serializable로 표시하는 단점
- 2. IronPython에 IronPython Console에서 클래스를 인스턴스화하는 데 문제가 있습니다.
- 3. 속성을 일시적으로 표시하면 형식이 중요합니까?
- 4. IronPython 예제
- 5. Ironpython : IEnumerator
- 6. IronPython 성능
- 7. IronPython + Emacs?
- 8. (IronPython) C#
- 9. 스크립트 인수와 Embedded IronPython
- 10. IronPython 코드가 실패하는 이유는 무엇입니까?
- 11. IronPython __setattr__ 및 __getattr__을 재정의합니다.
- 12. ToString을 기본 클래스에 가상으로 표시하면 어떻게됩니까?
- 13. IronPython : 어떻게 VS2008 IronPython 프로젝트를 만들 수 있습니까?
- 14. TDD with IronPython
- 15. 샌드 박스 IronPython?
- 16. IronPython 용 디버거
- 17. IronPython 및 Entity Framework
- 18. IronPython 응용 프로그램 배포
- 19. IronPython Excel RTDServer
- 20. IronPython 내장 메모리 누수
- 21. Ironpython 및 향후 진술
- 22. IronPython 및 pdb.set_trace()
- 23. Silverlight 4의 Ironpython 2.6.1
- 24. IronPython Linq 던지고 ArgumentTypeException
- 25. 런타임시 IronPython 컴파일러 버전
- 26. IronPython 메모리 사용량
- 27. IronPython Microsoft.Scripting 예외
- 28. SQLCLR 및 IronPython
- 29. IronPython 임베디드 IDE
- 30. IronPython asp.net IntelliSense
CodePlex 프로젝트는 DevHawk (@ jcao219의 답변에있는 솔루션과 동일한 개발자)에 의해 작성된 것 같습니다. 그의 [설명] (http://devhawk.net/tag/__clrtype__). 약간의 노력이 필요하지만 거기에는 좋은 것들이 많이 있습니다. – Wesley
fyi 프로젝트가 github로 이동되었습니다. https://github.com/IronLanguages/main/tree/ipy-2.7-maint/Languages/IronPython/Samples/ClrType –