From 7e2274219a956a71e7e1d33f2b31230c8f69bafd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Sep 2019 16:44:55 +0200 Subject: [PATCH] Fixed issue with some dynamic getters --- .../Translation/Models/ReadableMember.GetValue.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs index 3b347f1d..70b97559 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs @@ -45,13 +45,16 @@ namespace ComputeSharp.Shaders.Translation.Models DynamicMethod method = new DynamicMethod($"Get{Id}", typeof(object), new[] { typeof(object) }, DeclaringType); ILGenerator il = method.GetILGenerator(); - // Load the argument (the object instance) and cast it to the right type - il.Emit(OpCodes.Ldarg_0); - if (!IsStatic) il.Emit(DeclaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, DeclaringType); + // Load the argument (the object instance) and cast it to the right type, if needed + if (!IsStatic) + { + il.Emit(OpCodes.Ldarg_0); + il.Emit(DeclaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, DeclaringType); + } // Get the member value with the appropriate method - if (Property != null) il.EmitCall(OpCodes.Callvirt, Property.GetMethod, null); - else if (Field != null) il.Emit(OpCodes.Ldfld, Field); + if (Property != null) il.EmitCall(IsStatic ? OpCodes.Call : OpCodes.Callvirt, Property.GetMethod, null); + else if (Field != null) il.Emit(IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, Field); else throw new InvalidOperationException("Field and property can't both be null at the same time"); // Box the value, if needed