2010-04-12 6 views

답변

4

IronPython 개발자 중 한 명이이 예제를 제공하여 Python 클래스 (예 : Serializable)에 .NET 특성을 추가했습니다.

http://ironpython.codeplex.com/releases/view/36280#DownloadId=116513

+0

CodePlex 프로젝트는 DevHawk (@ jcao219의 답변에있는 솔루션과 동일한 개발자)에 의해 작성된 것 같습니다. 그의 [설명] (http://devhawk.net/tag/__clrtype__). 약간의 노력이 필요하지만 거기에는 좋은 것들이 많이 있습니다. – Wesley

+0

fyi 프로젝트가 github로 이동되었습니다. https://github.com/IronLanguages/main/tree/ipy-2.7-maint/Languages/IronPython/Samples/ClrType –

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에서 직렬화 가능으로 표시하십시오.

+0

.NET의 이진 직렬화를 사용해야합니다. – Pablo

+0

답장을 보내 주셔서 감사합니다. 관심이 있으시면 저의 두 번째 게시물을보십시오 :) – Pablo