我正在尝试使用YAXLib来处理XML,但每当我尝试使用int,double或float字段序列化类时,我都会崩溃。 YAXLib尝试调用
DefaultValue = MemberType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, new object[0]);
其中MemberType
是System.Int32
Type
个对象。
当然Int32没有要调用的构造函数。我试图了解这是如何起作用的。这是在较新版本的mono / C#中引入的吗?
这是在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)
答案 0 :(得分:1)
也许您想获得最新版本的YAX或其他什么?
我只是用Google搜索了YAXLib的MemberWrapper并找到了https://github.com/sinairv/YAXLib/blob/master/YAXLib/MemberWrapper.cs
有一种方法可以实现以下异常:
private void InitDefaultValue()
{
if(MemberType.IsValueType)
DefaultValue = MemberType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, new object[0]);
else
DefaultValue = null;
}
所以我稍微修改了它,并从一个控制台应用程序调用,它可以很好地返回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)));
}
这让我相信YAXLib与您正在使用的单声道版本之间存在一些不兼容性,因为它转换为“通过调用默认构造函数创建实例”,并且也应该在单声道下工作。