Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing failed due to missing parameterless constructor #799

Open
matthiasburger opened this issue Nov 11, 2021 · 3 comments
Open

Serializing failed due to missing parameterless constructor #799

matthiasburger opened this issue Nov 11, 2021 · 3 comments

Comments

@matthiasburger
Copy link

matthiasburger commented Nov 11, 2021

I wanted to use the XML-Serializer from .NET but I can't. This could be a problem that often can appear: missing parameterless constructor

import clr
import clrtype

clr.AddReference('System.Xml')
clr.AddReference('System.IO')

from System.Xml.Serialization import XmlElementAttribute, XmlSerializer
from System.IO import Stream, FileStream, FileMode


class NugetPackage(object):
    __metaclass__ = clrtype.ClrClass
    XmlElement = clrtype.attribute(XmlElementAttribute)

    def __init__(): pass ## doesn't work either....

    def __init__(self): ## isn't this one parameterless?
        self._id = None
        self._version = None
        self._repository = None
        self._authors = None
        self._owners = None
        self._tags = None
        self._description = None
        self._dependencies = []

    @staticmethod
    def read_info(nuspec):
        nuget_package_type = clr.GetClrType(type(NugetPackage))
        serializer = XmlSerializer(nuget_package_type)

        with FileStream(nuspec, FileMode.Open) as reader:
            return serializer.Deserialize(reader)

    @XmlElement('Id')
    def id(self, value):
        self._id = value

I tried everything.. but nothing works here. Any idea?

@slozier
Copy link
Contributor

slozier commented Nov 11, 2021

I think you've got the wrong type, should probably be:

nuget_package_type = clr.GetClrType(NugetPackage)

Although then it seems to fail on something else...

@matthiasburger
Copy link
Author

ahh that makes sense :)
still, it cannot reflect the type :/

Traceback (most recent call last):
  File "C:/Users/m.burger/PycharmProjects/ironwasp/main.py", line 18, in <module>
  File "C:/Users/m.burger/PycharmProjects/ironwasp/main.py", line 12, in print_hi
  File "C:\Users\m.burger\PycharmProjects\ironwasp\tools\nuget\package_manager.py", line 28, in read_info
SystemError: Fehler beim Reflektieren des Typs 'NugetPackage'.

@slozier
Copy link
Contributor

slozier commented Nov 15, 2021

Stack trace when running with -X:ExceptionDetail:

There was an error reflecting type 'NugetPackage'.
ImportTypeMapping at offset 2347 in file:line:column <filename unknown>:0:0

ImportElement at offset 175 in file:line:column <filename unknown>:0:0

ImportTypeMapping at offset 133 in file:line:column <filename unknown>:0:0

.ctor at offset 437 in file:line:column <filename unknown>:0:0

   at Microsoft.Scripting.Interpreter.NewInstruction.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Hosting.PythonCommandLine.RunFileWorker(String fileName)
   at IronPython.Hosting.PythonCommandLine.RunFile(String fileName)
   at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
   at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.StructModel.GetFieldModel(FieldInfo fieldInfo)
   at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
SystemError: There was an error reflecting type 'NugetPackage'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants