简体   繁体   English

C#MissingMethodException:找不到类型'System.Int32'上的构造方法

[英]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处理XML,但是每当我尝试使用int,double或float字段序列化一个类时,都会崩溃。 YAXLib attempts to call YAXLib尝试致电

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

where MemberType is a System.Int32 Type object. 其中MemberType是一个System.Int32 Type对象。

Of course Int32 doesn't have a constructor to call. 当然,Int32没有要调用的构造函数。 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#? 这是在较新版本的mono / C#中引入的吗?

This is in Unity3d, with Mono, .NET 2.0.5 这是在Unity3d中,带有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? 也许您想获取最新版本的YAX或其他内容?

I just googled for YAXLib MemberWrapper and found https://github.com/sinairv/YAXLib/blob/master/YAXLib/MemberWrapper.cs 我只是用谷歌搜索YAXLib MemberWrapper并找到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. 所以我稍加修改并从控制台应用程序调用,返回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. 这使我相信YAXLib和您使用的mono版本之间存在一些不兼容,因为它转换为“通过调用默认构造函数创建实例”,并且也应该在mono下工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Int32[] c# - System.Int32[] c# C#:无法将“System.Int64”类型的对象转换为“System.Int32”类型 - C#: Unable to cast object of type 'System.Int64' to type 'System.Int32' C# 无法将“system.string”类型的对象转换为“system.int32”类型 - C# unable to cast object of type 'system.string' to type 'system.int32' 在C#中,为什么“int”是System.Int32的别名? - In C#, why is “int” an alias for System.Int32? 是否可以在C#中创建System.Int32? - Is it possible to create System.Int32 in C#? 如何在C#中转义System.Int32消息 - How to escape message System.Int32 in C# 类型&#39;&lt;&gt; f__AnonymousType2`2 [System.String,System.Int32]&#39;必须声明一个默认(无参数)构造函数 - The type '<>f__AnonymousType2`2[System.String,System.Int32]' must declare a default (parameterless) constructor 类型&#39;System.Nullable`1 [System.Int32]&#39;的表达式不能用于类型&#39;System.Int32&#39;\\ r \\ n的构造函数参数名称:arguments [0] - Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0] 在 C# 中获取“参数字典包含方法 X 的不可空类型 'System.Int32' 的参数 'ID' 的 null 条目” - Getting “The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method X” in C# 找不到类型 xxx 的 System.MissingMethodException 构造函数 - System.MissingMethodException Constructor on type xxx not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM