Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cs-script+Newtonsoft.Json #377

Open
snikeguo opened this issue Jun 26, 2024 · 2 comments
Open

cs-script+Newtonsoft.Json #377

snikeguo opened this issue Jun 26, 2024 · 2 comments

Comments

@snikeguo
Copy link

snikeguo commented Jun 26, 2024

my program code:
step1:
loading user script and SerializeObject iterInstance(ClassA) ->a.json

public interface IIter
{
    int V{get;set;}
}
...
userAssembly=CSScript.RoslynEvaluator.CompileCode(userCode, CodeKind = SourceCodeKind.Script );
iterInstance=userAssembly.Create(class A  full Name);
···

    var settings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All//!!!** see  https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm
    };

    var str=JsonConvert.SerializeObject(iterInstance,Newtonsoft.Json.Formatting.Indented, settings);//Serialize class A
    File.WriteAllText("a.json", str);
}
...

the user code:

class A:IIter
{
   int V{get;set;}
  int V2{get;set;}
}

class B:IIter
{
   int V{get;set;}
  int V2{get;set;}
}
//ClasseA and B are the same except for their type names, which means they have the same fields, the same properties, and so on.

step2:
loading user script and DeserializeObject ,a.json->a(class A's object )

CSScriptLoadingUserScript(userCode);
a=JsonConvert.DeserializeObject(a.json's content);

I am unaware of the specific type of the user's a.json file, and each time CSScript generates an Assembly, its Type may change, resulting in a mismatch between the type specified in the json and the type of the Assembly.

How can we solve this problem?
image

@snikeguo
Copy link
Author

The problem has been solved: just specify the AssemblyName.
eg:
new CompileInfo { CodeKind = SourceCodeKind.Script, AssemblyName="aaa"}

@oleg-shilo
Copy link
Owner

Perfect. Thank you for sharing the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants