Skip to content

Instantly share code, notes, and snippets.

@0x070696E65
Created May 2, 2023 09:14
Show Gist options
  • Save 0x070696E65/8e96075198b36d609a854d703bb584f9 to your computer and use it in GitHub Desktop.
Save 0x070696E65/8e96075198b36d609a854d703bb584f9 to your computer and use it in GitHub Desktop.
四則演算
using CatSdk.Utils;
using System.Text.Json.Nodes;
using System.Runtime.Loader;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
var node = "https://hideyoshi.mydns.jp:3001";
async Task<string> GetDataFromApi(string _node, string _param)
{
var url = $"{_node}{_param}";
using var client = new HttpClient();
try
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
return await response.Content.ReadAsStringAsync();
}
throw new Exception($"Error: {response.StatusCode}");
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
}
var hash = "866069E90D1A0FFCA7E78B44518DE407D7A203BA8018C58B13A2D07067E19326";
var jsonString = await GetDataFromApi(node, $"/transactions/confirmed/{hash}");
var transaction = JsonNode.Parse(jsonString);
var source = Converter.HexToUtf8((string)transaction["transaction"]["message"]);
var options = CSharpParseOptions.Default
.WithLanguageVersion(LanguageVersion.CSharp8);
var syntaxTree = CSharpSyntaxTree.ParseText(source, options, "Calculator.cs");
var references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
};
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create(
"Calculator.dll",
new[] { syntaxTree },
references,
compilationOptions);
using var stream = new MemoryStream();
var emitResult = compilation.Emit(stream);
if (emitResult.Success)
{
stream.Seek(0, SeekOrigin.Begin);
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
var CalculatorType = assembly.GetType("Calculator");
if (CalculatorType == null) throw new Exception("Type not found");
var addMethod = CalculatorType.GetMethod("Add");
var subtractMethod = CalculatorType.GetMethod("Subtract");
var multiplyMethod = CalculatorType.GetMethod("Multiply");
var divideMethod = CalculatorType.GetMethod("Divide");
var a = 10;
var b = 5;
if (addMethod == null || subtractMethod == null || multiplyMethod == null || divideMethod == null)
throw new Exception("Method not found");
var addResult = (int)(addMethod.Invoke(null, new object[]{a, b}) ?? throw new InvalidOperationException());
var subtractResult = (int)(subtractMethod.Invoke(null, new object[]{a, b}) ?? throw new InvalidOperationException());
var multiplyResult = (int)(multiplyMethod.Invoke(null, new object[]{a, b}) ?? throw new InvalidOperationException());
var divideResult = (int)(divideMethod.Invoke(null, new object[]{a, b}) ?? throw new InvalidOperationException());
Console.WriteLine($"Calculator.Add(10, 5) = {addResult}");
Console.WriteLine($"Calculator.Subtract(10, 5) = {subtractResult}");
Console.WriteLine($"Calculator.Multiply(10, 5) = {multiplyResult}");
Console.WriteLine($"Calculator.Divide(10, 5) = {divideResult}");
}
else
{
var failures = emitResult.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (var diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment