none
How to call "__makeref", "__refvalue", "__reftype" and "__arglist" in VB.NET? RRS feed

All replies

  • You mean something like this. 

    AFAIK has C# no equivalent of this VB code. 

    Likewise in C# is it not recommended to use it. 

    However, it is no magic so with some tricks it is probably available in C# too.


    Success
    Cor


    Saturday, July 29, 2017 12:10 PM
  • Many thanks!

    First I know that SetValueDirect isn't recommanded directly called. But in my mind, is there anyway to implement such thing?

    e.g:

    I still wanna use something like TypedReference.XXX, but cannot succeed....:( (In C#, __makeref(xxx) is to make a type instance to be "referencable").


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    Saturday, July 29, 2017 1:30 PM
  • (In C#, __makeref(xxx) is to make a type instance to be "referencable").

    See: http://www.dotnetheaven.com/article/boxing-and-unboxing-in-vb.net

    Saturday, July 29, 2017 10:25 PM
  • Hello Acamar:

    You just gave me unboxing and boxing. But as what I said earlier——I don't mean I wanna boxing or unboxing but just using __makeref/__refvalue……to avoid boxing or unboxing like C# in VB.NET.

    Any ideas or suggestions? Maybe I must use IL?


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    Sunday, July 30, 2017 2:30 AM
  • You just gave me unboxing and boxing. But as what I said earlier——I don't mean I wanna boxing or unboxing but just using __makeref/__refvalue……to avoid boxing or unboxing like C# in VB.NET.

    Boxing/unboxing is the .Net version of makeref/refvalue.  What are you trying to do that boxing and unboxing doesn't achieve?

    Sunday, July 30, 2017 2:59 AM
  • This is a senario in C#:

    namespace CSharpScarelyUsedKeyWordsTest
    {
        using Microsoft.VisualStudio.TestTools.UnitTesting;
        using System.Linq;
    
        [TestClass]
        public class TestForRefValue
        {
            #region Test for fetching values and increase itself
            /// <summary>
            /// Create a structure called "MyStruct"
            /// </summary>
            private MyStruct[] mystructs = null;
            /// <summary>
            /// Each time it will create a huge array of mystruct values for test
            /// </summary>
            [TestInitialize]
            public void Initialization()
            {
                mystructs = new MyStruct[10000];
            }
            /// <summary>
            /// Notice that this has mocked a case: Since we don't know what "T" is, so we have to box it to convert it to a specific struct and then do coping with its inner values and then re-convert it back to object and do a cast of T as the return type for each array's value.
            /// </summary>
            private void ChangeValue<T>(ref T value)
            {
                if (typeof(T) == typeof(MyStruct))
                {
                    MyStruct mv = (MyStruct)(object)value;
                    mv.I++;
                    value = (T)(object)mv;
                }
            }
            [TestMethod]
            public void OriginalFetchAndUnboxing()
            {
                for (int i = 0; i < mystructs.Length; i++)
                {
                    ChangeValue(ref mystructs[i]);
                }
                Assert.IsFalse(mystructs.Any(ms => ms.I == 0));
            }
        
            private void ChangeValueByRefVal<T>(ref T value)
            {
                if (typeof(T) == typeof(MyStruct))
                {
                    __refvalue(__makeref(value), MyStruct).I++;
                }
            }
            [TestMethod]
            public void FetchValueAndUseRefValue()
            {
                for (int i = 0; i < mystructs.Length; i++)
                {
                    ChangeValueByRefVal(ref mystructs[i]);
                }
                Assert.IsFalse(mystructs.Any(ms => ms.I == 0));
            }
            #endregion
        }
    }
    

    Plz compare "ChangeValue" and "ChangeValueByRefVal" method——this doesn't just need boxing or unboxing and sometimes the speed is faster than boxing or unboxing. I just do some unit tests to check the result. So the latter method will be more simple in its syntaxes.

    I just don't like boxing or unboxing for such a huge array of customized structure (MyStructure has ONLY one get set property called "I").

    So my question is how to translate this to VB.NET?

     private void ChangeValueByRefVal<T>(ref T value)
            {
                if (typeof(T) == typeof(MyStruct))
                {
                    __refvalue(__makeref(value), MyStruct).I++;
                }
            }

    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    Sunday, July 30, 2017 5:25 AM
  • Have you any idea how much boxing and unboxing takes?

    It was once always a discussion between Jon Skeet and me in the Microsoft newsgroups and most persons said Jon is right. Until the day I made a sample. It took for endless unboxing less than a millisecond. 

    The phrase that it takes time is from the time of the 20286 processor. 

    Currently are in fact all Integers on a 64Bit computer first transalet to Int64 because that is the register (accumulator) size it has to be used on that kind of computers. 

    And you won't believe it, you will never recognize it. 


    Success
    Cor



    Sunday, July 30, 2017 9:43 AM
  • Cor:

    1) First I agree with what you said:I did an experiment and I found that when the array size is smaller than 100000, the __refvalue is faster than boxing/unboxing. However when the size is larger than that, the boxing/unboxing is faster. I don't know why (My machine is a 32-bit one, with 4GB free memory, Intel Core i5 4200U). 

    You can download the sample from here: https://code.msdn.microsoft.com/Scarcely-Used-Key-Words-in-673ee4f3?redir=0.

    After compiling and doing testing for several times, you can see the result from the Test Result Panel. My code sample is easy to tell you how to use some "scarcely-used undocumented key words in C#", I tend to publish two versions (C# and VB.NET), but I cannot find how to do that in VB.NET?

    2) So let's come back to my core question:Isn't there anyway to translate that to VB.NET?

    3) PS:I'm interested in your example, can you show that here?


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report





    Sunday, July 30, 2017 12:50 PM
  • Cor:


    That is not in my interest so I'm a bad one to give you that sample. Only when I read your question and investigated it a little bit I thought. Uh, that is probably a part the .Net team spoke about (already a while ago) when they where busy with C# and Dynamic keyword for parity with VB Option Strict off and that was more than only dynamic behaviour.  

    However, maybe you have luck (and therefore I write his name) and sees Dave Doknjas this, if he does not know it, then you probably can forget it. 


    Success
    Cor

    Sunday, July 30, 2017 3:05 PM

  • However, maybe you have luck (and therefore I write his name) and sees Dave Doknjas this, if he does not know it, then you probably can forget it. 


    Success
    Cor

    Thanks for your vote of confidence Cor - however, just when I was feeling pretty smug about having seen it all in VB and C#, I have to admit that I've never heard of most of these (except for _arglist).

    Wish I could help!

    For now, I have a few things to add to my 'to do' list.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Sunday, July 30, 2017 9:30 PM
  • So that's the reason why I posted this question and wrote the sample code about these undocumented key words, because Microsoft hasn't told us more details about them yet. I have to do many experiments to prove something....

    So Dave Doknjas:According to what Core's said, what do you think of this? (I mean I suspect whether there has anything to do with TypedReference……)?


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report

    Monday, July 31, 2017 2:08 AM
  • Hi ThankfulHeart,

    About this question, I suggest you to go to feedback to get more helpful help, since Microsoft don't provide more detailed information about this, please refer to:

    https://connect.microsoft.com/continue.aspx?pageType=2&regType=2&cru=%2FVisualStudio%2Ffeedback%2FCreateFeedback.aspx&cu=

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 16, 2017 8:55 AM
    Moderator