diff --git a/Packages/com.quabug.any-processor/CodeGen/Extension/Extensions.cs b/Packages/com.quabug.any-processor/CodeGen/Extension/Extensions.cs index 78c5a37..f708f70 100644 --- a/Packages/com.quabug.any-processor/CodeGen/Extension/Extensions.cs +++ b/Packages/com.quabug.any-processor/CodeGen/Extension/Extensions.cs @@ -215,13 +215,20 @@ public static void ReplacePropertyGetterByFieldMethod(this PropertyDefinition pr // IL_0001: ldfld class [AnySerialize]AnySerialize.AnyValue`1 TestAnySerialize::_value // IL_0006: callvirt instance !0/*object*/ class [AnySerialize]AnySerialize.AnyValue`1::get_Value() // IL_000b: ret + + // after(value type) + // IL_0000: ldarg.0 // this + // IL_0001: ldflda valuetype AnySerialize.AnyDateTime AnySerialize.A::Field + // IL_0006: call instance valuetype [netstandard]System.DateTime AnySerialize.AnyDateTime::get_Value() + // IL_000b: ret var instructions = property.GetMethod!.Body!.Instructions; var getValueMethod = serializedField.FieldType!.GetMethodReference(fieldMethodName, logger); getValueMethod = property.Module!.ImportReference(getValueMethod!); + var isValueTypeField = serializedField.FieldType.IsValueType; instructions!.Clear(); instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); - instructions.Add(Instruction.Create(OpCodes.Ldfld, serializedField)); - instructions.Add(Instruction.Create(OpCodes.Callvirt, getValueMethod!)); + instructions.Add(Instruction.Create(isValueTypeField ? OpCodes.Ldflda : OpCodes.Ldfld, serializedField)); + instructions.Add(Instruction.Create(isValueTypeField ? OpCodes.Call : OpCodes.Callvirt, getValueMethod!)); instructions.Add(Instruction.Create(OpCodes.Ret)); } diff --git a/Packages/com.quabug.any-serialize/CodeGen/Extension.cs b/Packages/com.quabug.any-serialize/CodeGen/Extension.cs index 4681331..ce855d4 100644 --- a/Packages/com.quabug.any-serialize/CodeGen/Extension.cs +++ b/Packages/com.quabug.any-serialize/CodeGen/Extension.cs @@ -13,9 +13,11 @@ public static class Extension public static TypeReference? CreateConcreteTypeFrom(this Container container, TypeReference type) { var def = type.Resolve(); - if (def == null || def.IsAbstract || def.GetConstructors()!.FirstOrDefault(ctor => !ctor.HasParameters) == null) return null; + if (def == null) return null; + if (def.IsAbstract) return null; + if (!def.IsValueType && def.GetConstructors()!.FirstOrDefault(ctor => !ctor.HasParameters) == null) return null; if (type.IsConcreteType()) return type; - + if (type is not GenericInstanceType genericType) throw new ArgumentException($"{nameof(type)}({type}) must be a {nameof(GenericInstanceType)}", nameof(type));