/
LavrGov
/
csharp_ob
Обзор
Документация
Войти
/
LavrGov
/
csharp_ob
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
RunCode/Program.cs
245 строк
6 KB
Lavrentiy
Add mem_cpy
03 июн 2025, 22:55
03 июн 2025, 22:55
7fc7f70
Код
Авторство
О чём код?
// See https://aka.ms/new-console-template for more information using System.Reflection;using System.Runtime.CompilerServices; using System.Runtime.Loader; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using System.Runtime.InteropServices; using RoslynCore; namespace RoslynCore { [StructLayout(LayoutKind.Sequential)] public class Helper { private int data = 200; private int data2 = 255; //private double data4 = 4.23; private string data3= "test"; //private string data3; // Есть словарь public IntPtr getInt() { unsafe { //Console.WriteLine(sizeof(string)); fixed (int * data1 = &data) //GCHandle gh = GCHandle.Alloc(data); return (IntPtr)(data1); } } public IntPtr getDouble() { unsafe { //Console.WriteLine(sizeof(string)); fixed (int * data1 = &data) //GCHandle gh = GCHandle.Alloc(data); return (IntPtr)(data1); } } public void writeValue() { Console.WriteLine(data); Console.WriteLine(data2); Console.WriteLine(data3); } } } class Program { private static void Main() { //Console.WriteLine("Test"); string myCode = @"using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace RoslynCore { [StructLayout(LayoutKind.Sequential)] public class Helper { private int data1 = 100; private int data2 = 234; private string data3; public Helper(int data12, int data13, string data14) { data1 = data12; data2 = data13; data3 = data14; } public static Helper createHelepr(int data1, int data2, string data3) { return new Helper(data1, data2, data3); } public int getInt() { return data1; } public IntPtr getIntPtr() { unsafe { fixed (int * data = &data1) return (IntPtr)(data); } } } }"; RunAnotherCode newCode = new RunAnotherCode(myCode); object myResult = newCode .SetNameSpace("RoslynCore") .SetClassName("Helper") .RunCode<RoslynCore.Helper>("createHelepr", 100, 234, "gLast"); IntPtr src = (IntPtr)newCode.GetCode("getIntPtr", myResult); Console.WriteLine(src); Helper newHelper = new Helper(); IntPtr dst = newHelper.getInt(); IntPtr getDouble = newHelper.getDouble(); Console.Write("Int ptr: "); Console.WriteLine(dst); Console.Write("double ptr: "); Console.WriteLine(getDouble); Console.WriteLine("Old value: "); newHelper.writeValue(); RunAnotherCode.mem_test(dst, src); Console.WriteLine("New value"); newHelper.writeValue(); Console.WriteLine("------------"); //IntPtr data = newHelper.getInt(); //RunAnotherCode.mem_test(dst, dst); //RunAnotherCode.memcpy(dst, dst); //Console.WriteLine(newHelper.getInt()); //newCode.RunCode(); } } public partial class MemCpy { [LibraryImport("/Users/lavrentijmarkov/projects/dot_project/RunCode/RunCode/mem_cpy/target/debug/libmem_cpy.dylib")] public static partial void ffi_mem_num(IntPtr dst, IntPtr src, int size); } class RunAnotherCode { private string fileSo = "file.dylib"; //private string memdll = ""; private Assembly myAsm; private string nameSpace; private string className; public RunAnotherCode(string newCode) { var someCode = newCode; var parseData = SyntaxFactory.ParseSyntaxTree(someCode); var systemReflection = typeof(object).GetTypeInfo().Assembly.Location; var systemRef = MetadataReference.CreateFromFile(systemReflection); var compilation = CSharpCompilation.Create(fileSo) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe:true)) .AddReferences(systemRef) .AddSyntaxTrees(parseData); string path = Path.Combine(Directory.GetCurrentDirectory(), fileSo); var emitResult = compilation.Emit(path); myAsm = AssemblyLoadContext.Default.LoadFromAssemblyPath(path); //var compileAns = parseData.GetRoot(); } public RunAnotherCode SetClassName(string className) { this.className = className; return this; } public RunAnotherCode SetNameSpace(string nameSpace) { this.nameSpace = nameSpace; return this; } public object GetCode(string methodName, object obj) { string type = nameSpace + "." + className; object result = myAsm .GetType(type) .GetMethod(methodName) .Invoke(obj, null); return result; } public object RunCode<T>(string methodName, params object[] allData) { string type = nameSpace + "." + className; object result = myAsm .GetType(type) .GetMethod(methodName) .Invoke(null, allData); //Console.WriteLine($"Result is {result}") return result; } public void SetNewNameSpace(string newNameSpavce) { nameSpace = newNameSpavce; } public static void memcpy<T, U>(T dst, U src) { Int32 sizeType = Unsafe.SizeOf<T>(); GCHandle dst_gc = GCHandle.Alloc(dst); GCHandle src_gc = GCHandle.Alloc(src); IntPtr dst_ptr = GCHandle.ToIntPtr(dst_gc); IntPtr src_ptr = GCHandle.ToIntPtr(src_gc); unsafe { //int test_Data = 123; //int* getData = &test_Data; //IntPtr ptr2 = (IntPtr)getData; //MemCpy.ffi_mem_num(ptr2, ptr2, 4); } Console.WriteLine(dst_ptr); MemCpy.ffi_mem_num(dst_ptr, src_ptr, sizeType); } public static void mem_test(IntPtr data1, IntPtr data2) { //sizeof(double); Console.Write("double: "); Console.WriteLine(Unsafe.SizeOf<double>()); MemCpy.ffi_mem_num(data1, data2, 24); } }