Example #1
0
        public static void Main(string[] args)
        {
            var f = (MetaField)typeof(string).GetAnyField("m_firstChar");

            Console.WriteLine("{0:A}", f);

            var l = Runtime.AllocObject <List <int> >();

            l.Add(1);
            Console.WriteLine(l.Count);
            Console.WriteLine(GCHeap.GCCount);
            GC.Collect();
            Console.WriteLine(GCHeap.GCCount);
        }
Example #2
0
        /// <summary>
        ///     Creates an instance of <typeparamref name="T" /> from <paramref name="mem" />.
        ///     If <typeparamref name="T" /> is a reference type, <paramref name="mem" /> should not contain
        ///     object internals like its <see cref="MethodTable" /> pointer or its <see cref="ObjHeader" />; it should
        ///     only contain its fields. This memory does not need to be freed, as it is GC allocated.
        /// </summary>
        /// <param name="mem">Memory to load from</param>
        /// <typeparam name="T">Type to load</typeparam>
        /// <returns>An instance created from <paramref name="mem" /></returns>
        public static T AllocRaw <T>(byte[] mem)
        {
            T value = default;

            Pointer <byte> addr;

            if (Runtime.Info.IsStruct <T>())
            {
                addr = Unsafe.AddressOf(ref value).Cast();
            }
            else
            {
                value = Runtime.AllocObject <T>();
                addr  = Unsafe.AddressOfFields(ref value).Cast();
            }

            addr.WriteAll(mem);

            return(value);
        }