[英]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.