zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Initializing a list with type T of a complex type not working #15

Closed rogerfar closed 6 years ago

rogerfar commented 6 years ago

When trying to execute the following code:

var test= new ComplexType();

var list = new List<ComplexType>();

ComplexType is just a class in this example.

it fails at the 2nd line with the message: Oops! The type could not be found. Only common type are registered by default. Make sure the type you are using is registered: https://github.com/zzzprojects/Eval-Expression.NET/wiki/EvalContext-Register-&-Unregister

JonathanMagnan commented 6 years ago

Hello @yesman85 ,

Did you register your ComplexType via RegisterType or RegisterAssembly` ? I tried some very basic scenario which seemed to work.

public class BaseType
{

}

public class ComplexType : BaseType
{
    public int ColumnInt { get; set; }
}

EvalManager.DefaultContext.RegisterType(typeof(ComplexType));

var item = Eval.Execute<ComplexType>("var test = new ComplexType();");
var list = Eval.Execute<List<ComplexType>>("var list = new List<ComplexType>();");
rogerfar commented 6 years ago

Yes, if I execute both lines in a single Execute it will break on the 2nd line, not the first one, indicating that the type is actually found.

JonathanMagnan commented 6 years ago

Strange, it seems to work on our side even in a single execute

Do you think you could try the following code?

public class BaseType
{

}

public class ComplexType : BaseType
{
    public int ColumnInt { get; set; }
}

EvalManager.DefaultContext.RegisterType(typeof(ComplexType));

var test = Eval.Execute(@"
var test= new ComplexType();
var list = new List<ComplexType>();
");
rogerfar commented 6 years ago

It works, BUT when I add: EvalManager.DefaultContext.RegisterDomainAssemblies(); it stopped working, even when I remove EvalManager.DefaultContext.RegisterType(typeof(ComplexType));.

I also noticed that my complex type is a partial class, maybe that's the culprit?

JonathanMagnan commented 6 years ago

Do you think you could provide a small example with this issue? By example in a new console application?

It will make easier on our side to fix it than trying to reproduce the problem.

It seems to work on partial class and with RegisterDomainAssemblies.

rogerfar commented 6 years ago

I tried it in a separate app but it worked then, but now I found out that it's not the complex type that's the issue but the List.

When I do var list = new System.Collections.Generic.List<ComplexType>(); it works, so there must be another assembly with the name List somewhere, although Visual Studio is not suggesting there is.

JonathanMagnan commented 6 years ago

Hello @yesman85 ,

Thank a lot,

We released the v2.4.2 which blacklist the class List when registering type via RegisterDomainAssemblies. A better solution will need to be found later.

Let me know if your expression can now work correctly.

Best Regards,

Jonathan

rogerfar commented 6 years ago

Yes it works good now! Thanks.