简体   繁体   中英

C# MissingMethodException: Constructor on type 'System.Int32' not found

I'm trying to use YAXLib to work with XML, but whenever I try to serialize a class with an int, double, or float field, I get a crash. YAXLib attempts to call

DefaultValue = MemberType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, new object[0]);

where MemberType is a System.Int32 Type object.

Of course Int32 doesn't have a constructor to call. I'm trying to understand how this worked in the first place. Is this something that was introduced in a newer version of mono/C#?

This is in Unity3d, with Mono, .NET 2.0.5

MissingMethodException: Constructor on type 'System.Int32' not found.
System.MonoType.InvokeMember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/MonoType.cs:398)
System.Type.InvokeMember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Type.cs:1149)
YAXLib.MemberWrapper.InitDefaultValue () (at Assets/Vendor/YAXLib/MemberWrapper.cs:691)
YAXLib.MemberWrapper.InitInstance () (at Assets/Vendor/YAXLib/MemberWrapper.cs:682)
YAXLib.MemberWrapper..ctor (System.Reflection.MemberInfo memberInfo, YAXLib.YAXSerializer callerSerializer) (at Assets/Vendor/YAXLib/MemberWrapper.cs:122)
YAXLib.YAXSerializer+<GetFieldsToBeSerialized>c__Iterator1.MoveNext () (at Assets/Vendor/YAXLib/YAXSerializer.cs:2815)
YAXLib.YAXSerializer.SerializeBase (System.Object obj, System.Xml.Linq.XName className) (at Assets/Vendor/YAXLib/YAXSerializer.cs:722)
YAXLib.YAXSerializer.SerializeBase (System.Object obj) (at Assets/Vendor/YAXLib/YAXSerializer.cs:609)
YAXLib.YAXSerializer.SerializeXDocument (System.Object obj) (at Assets/Vendor/YAXLib/YAXSerializer.cs:539)
YAXLib.YAXSerializer.Serialize (System.Object obj) (at Assets/Vendor/YAXLib/YAXSerializer.cs:349)
DataHandler..ctor () (at Assets/DataWrangling/DataHandler.cs:36)
YAXTestHarness.Start () (at Assets/DataWrangling/YAXTestHarness.cs:8)

Maybe you want to get a latest version of YAX or something?

I just googled for YAXLib MemberWrapper and found https://github.com/sinairv/YAXLib/blob/master/YAXLib/MemberWrapper.cs

there is that method you have exception in implemented as following:

    private void InitDefaultValue()
    {
        if(MemberType.IsValueType)
            DefaultValue = MemberType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, new object[0]);
        else
            DefaultValue = null;
    }

so I slightly modified it and called from a console app and it works just fine returning 0.

    private static object InitDefaultValue(Type MemberType)
    {
        if (MemberType.IsValueType)
            return MemberType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, new object[0]);
        else
            return null;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(InitDefaultValue(typeof(System.Int32)));
    }

this leads me to believe that there is some incompatibility between YAXLib and mono versions you are using, as it translates to "create instance by calling default constructor" and should work under mono too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM