C#C
C#2y ago
TheSnowOwl

Trying to compile a script at runtime

im trying to compile a script at runtime using CSharpCompilation.Emit

this is the script i want to compile
namespace WinterRose.Generated.Serializer_WinterRose_Vectors_Vector2
{
    using System;
    using WinterRose.Vectors;
    using WinterRose.Serialization.Workers;
    using WinterRose.Serialization;
    using System.Collections.Generic;
    using System.Text;

    public class GeneratedSerializer_Vector2
    {
        public StringBuilder Serialize(WinterRose.Vectors.Vector2 obj, SerializerSettings settings, Int32 depth)
        {
            StringBuilder builder = new();
            builder.Append("@0VmVjdG9yMi0tV2ludGVyUm9zZS5WZWN0b3JzLS1XaW50ZXJSb3NlLCBWZXJzaW9uPTEuMy4zLjEsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbA==");
            builder.Append($"#0x|0{SnowSerializerWorkers.SerializeField(obj.x, "x", typeof(Single), depth, settings)}");
            builder.Append($"#0y|0{SnowSerializerWorkers.SerializeField(obj.y, "y", typeof(Single), depth, settings)}");
            return builder;
        }

        public Vector2 Deserialize(string data, SerializerSettings settings, Int32 depth)
        {
            Vector2 result = new();
            Dictionary<string, string?> values = new();
            string[] splits = data.Split($"#{depth}");
            foreach (string item in splits)
            {
            string[] split = item.Split("|{depth}");
            values.Add(split[0], split[1] ?? null);
             }
            foreach (var item in values)
            {
            switch (item.Key)
            {
                case "x":
            result.x = SnowSerializerWorkers.DeserializeField<Single>(values["x"], typeof(Single), depth);
            break;
                case "y":
            result.y = SnowSerializerWorkers.DeserializeField<Single>(values["y"], typeof(Single), depth);
            break;
             }
             }
            return result;
        }
    }
}


certainly it would be a lot better to actually use source generators, however my project requires the ability to generate code from scripts that are also compiled at runtime. however this specific script is causing me errors that i cant seem to find. errors like switch statments missing, commas missing, semicolons missing, and like 200 of these, so i really have no idea

i hope someone here has some ideas of what i could do. i already tried restarting visual studio, that didnt work
Was this page helpful?