How to "insert" a value WITHOUT using boxing support in a programming language?

I want to add a value without using any built-in .NET support.

That is, given the value of the enumeration, I want an object of a reference type that represents this value and its type.

This is a subtitle that allows you to pass enumerated values ​​from the last coherent clean C ++ code, this is a possible solution, so I'm not looking for how to use, for example. C # boxing (which is easy and doesn't matter in many ways).

The following code gives ...

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 class FieldInfothat is required for signing MakeTypedReference, I get an exception saying that the argument is not RuntimeFieldInfo.

Bad 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 );
        }
    }
}

The answer that will help make the above code “work” will be very enjoyable.

An answer that points to another way to achieve boxing (maybe it was completely and completely wrong - just experimental) would also be very nice! :-)

: : , # (object)v. enum ToObject, , , .NET, ++ 32- . ++ , arg, . MessageBox.Show , , .NET , , .

+3
3

, , TypedReference, __makeref() #. :

using System.Windows.Forms;
using System;
using MethodInfo = System.Reflection.MethodInfo;

namespace hello
{
    class Startup
    {
        static void Main(string[] args)
        {
            Type stringType = typeof(string);
            Type messageBoxType = typeof(MessageBox);
            Type mbButtonsType = typeof(MessageBoxButtons);
            Type[] argTypes = { stringType, stringType, mbButtonsType };
            MethodInfo showMethod = messageBoxType.GetMethod("Show", argTypes);

            var OkBtn = MessageBoxButtons.OK;
            TypedReference tr = __makeref(OkBtn);
            object mbOkBtn = TypedReference.ToObject(tr);

            object[] mbArgs = { "Hello, world!", "Reflect-app:", mbOkBtn };

            showMethod.Invoke(null, mbArgs);
        }
    }
}
+7

, '__makeref', TypedReference.MakeTypedReference

var v = MessageBoxButtons.OK;
var tr = __makeref(v);
var obj = TypedReference.ToObject(tr);
var s = obj.ToString();

//  s = "OK"
+3

, - Object, Framework, , , , . , Debug.Print, , , , a System.Array[], , , . , , Debug.Print , (,

Cat Meowser = new SiameseCat();
Animal Ralph = new PersianCat();
IPettable Mindy = new MixedBreedCat();
Debug.Print("{0:T} {1:T} {2:T}", Meowser, Ralph, Mindy);

Debug.Print System.Array[3] Cat[1], Animal[1] IPettable[1].

, , , ; , . , . , , , , , .

0

Source: https://habr.com/ru/post/1771227/


All Articles