C
Join ServerC#
help
✅ VerificationException
RRuttie12/23/2022
A friend of mine has a method that uses the Reflection.Emit namespace to create a method with the following IL:
What will happen if the field we try to access here is private/internal?
According to my friend, the code runs fine, but according to ChatGPT the following will happen:
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
if (fi.FieldType.IsValueType && typeof(TField) == typeof(object))
gen.Emit(OpCodes.Unbox_Any, fi.FieldType);
gen.Emit(OpCodes.Stfld, fi);
gen.Emit(OpCodes.Ret);
What will happen if the field we try to access here is private/internal?
According to my friend, the code runs fine, but according to ChatGPT the following will happen:

RRuttie12/24/2022
Now that I've got the time to check it:
It appears I can access private fields I shouldn't have access to.
It appears I can access private fields I shouldn't have access to.
RRuttie12/24/2022
However, on .NET 4.7.2, I get a
Does anyone know how I could get around this?
System.Security.VerificationException
.Does anyone know how I could get around this?
RRuttie12/24/2022
It only happens on the method call.
Ccanton712/24/2022
You can do the usual trick of using reflection?
Ccanton712/24/2022
Compiled expressions also have no problem accessing private fields IIRC, although they're compiled to IL
RRuttie12/24/2022
well yeah, but this approach has it's benefits over reflection

RRuttie12/24/2022
'Compiled expressions'?
AAntonC12/24/2022
it's one level of abstraction above il emission, it works with expression trees
RRuttie12/24/2022
How would you use such an expression in this case?
AAntonC12/24/2022
you would build up the syntax tree effectively, and then compile it using that method
AAntonC12/24/2022
there are factory functions for making syntax nodes
RRuttie12/24/2022
damn that's slow to build

RRuttie12/24/2022
but it works for .net472 so it's the only option ig
RRuttie12/24/2022
I'm still curious about this though
AAntonC12/24/2022
idk you can browse their sources and see how that compile method is implemented
AAntonC12/24/2022
it shouldn't be intrinsic
RRuttie12/24/2022
The problem is, the compile method doesn't throw it, it only gets thrown when trying to execute the method
AAccord12/25/2022
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.