gpt4 book ai didi

c# - 序列化在 ScriptEngine session 中创建的类型

转载 作者:行者123 更新时间:2023-11-30 17:53:39 25 4
gpt4 key购买 nike

考虑以下控制台应用程序:

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Reflection.Emit;
using System.Reflection;
using Roslyn.Scripting.CSharp;

class Program
{
static void Main(string[] args)
{
var dynAssembly = AssemblyBuilder
.DefineDynamicAssembly(
new AssemblyName("TestAssembly"),
AssemblyBuilderAccess.RunAndSave);
var dynModule = dynAssembly.DefineDynamicModule("TestModule");
var typeBuilder = dynModule.DefineType("Test", TypeAttributes.Public);
var dynamicType = typeBuilder.CreateType();
var serializer = new XmlSerializer(dynamicType);
var sBuilder = new StringBuilder();
serializer.Serialize(
new StringWriter(sBuilder),
Activator.CreateInstance(dynamicType));
Console.WriteLine(sBuilder.ToString());

var engine = new ScriptEngine();
engine.AddReference(Type.GetType("System.Object").Assembly.Location);
engine.ImportNamespace("System");
var roslynType = engine
.CreateSession()
.Execute<Type>("public class Test{public Test(){}};typeof(Test)");
serializer = new XmlSerializer(roslynType); // FileNotFoundException
}
}

当尝试实例化 XmlSerializer 并将 roslynType 作为参数传递时,发生以下运行时异常:

Could not load file or assembly 'ℛ*875699ae-0282-43b7-8ca3-a2f223a3d567-#1UD.XmlSerializers' or one of its dependencies. The filename, directory name, or volume label syntax is incorrect.

FusionLog 显示如下:

=== Pre-bind state information ===
LOG: User = ***
LOG: DisplayName = ℛ*12d7c994-3636-4081-93d3-6bfe43cc8988-#1UD.XmlSerializers
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: ℛ*12d7c994-3636-4081-93d3-6bfe43cc8988-#1UD.XmlSerializers |
Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the
assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more
information and common solutions to this issue.
LOG: Appbase = ***
LOG: Initial PrivatePath = NULL
Calling assembly : System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: ***
LOG: Using host configuration file:
LOG: Using machine configuration file from
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial,
or location-based assembly bind).
LOG: Attempting download of new URL
file:///c:/users/userName/documents/visual studio 2012/
Projects/ConsoleApplication147/ConsoleApplication147/bin/Debug/
ℛ*12d7c994-3636-4081-93d3-6bfe43cc8988-#1UD.XmlSerializers.DLL.
ERR: A fatal error occurred when retrieving next codebase for download (hr = 0x8007007b).

是否可以序列化 roslynType 的实例?如果是这样,我缺少的步骤是什么?是否有完全不同的方法?

最佳答案

你是running into this problem .

the other answers之一在该页面上建议使用以下内容实例化您的 XmlSerializer

var serializer = XmlSerializer.FromTypes(new[] { typeof(MyType) })[0];

关于c# - 序列化在 ScriptEngine session 中创建的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17083497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com