I want to box a value without using whatever .NET language's built-in support for that.
That is, given an enum value I want an reference type object that represents that value and its type.
This is a subgoal of being able to pass enum values from late binding pure C++ code, a possible solution of that, so, I'm not looking for how to use e.g. C# boxing (that's easy, and irrelevant in so many ways).
The following code yields ...
c:\projects\test\csharp\hello\main.cs(6,26): error CS0122: 'System.Reflection.RuntimeFieldInfo' is inaccessible due to its protection level
However, using the more documented FieldInfo
class, which is what the signature of MakeTypedReference
requires, I get an exception saying that the argument isn't RuntimeFieldInfo
.
The unsuccessful code, experimental, C#:
using System.Windows.Forms;
using Type = System.Type;
using TypedReference = System.TypedReference;
using MethodInfo = System.Reflection.MethodInfo;
using FieldInfo = System.Reflection.FieldInfo;
using RuntimeFieldInfo = System.Reflection.RuntimeFieldInfo;
namespace hello
{
class Startup
{
static void Main( string[] args )
{
Type stringType = typeof( string );
Type messageBoxType = typeof( MessageBox );
Type mbButtonsType = typeof( MessageBoxButtons );
Type mbIconType = typeof( MessageBoxIcon );
Type[] argTypes = { stringType, stringType, mbButtonsType };// }, mbIconType };
MethodInfo showMethod = messageBoxType.GetMethod( "Show", argTypes );
// object mbOkBtn = (object) (MessageBoxButtons) (0);
TypedReference tr = TypedReference.MakeTypedReference(
mbButtonsType,
new RuntimeFieldInfo[]{ mbIconType.GetField( "OK" ) }
);
object mbOkBtn = TypedReference.ToObject( tr );
object[] mbArgs = { "Hello, world!", "Reflect-app:", mbOkBtn };
showMethod.Invoke( null, mbArgs );
}
}
}
An answer that helps making the above code "work" would be very nice.
An answer that points out another way to achieve boxing (perhaps the above is completely and utterly wrong? - it's just experimental) would also be very nice! :-)
EDIT: Clarification: essentially I'm after the same as C# (object)v
yields. I have tried the enum ToObject
method, but unfortunately while that presumably works OK within .NET, on the C++ side I just get back the 32-bit integer value. The problem on the C++ side is that passing an integer as third arg of e.g. MessageBox.Show
just fails, presumably because the default binder on the .NET side doesn't convert it to enum type, so I suspect a reference object of suitable type is needed for actual argument.