Skip to content

C# Types Management

Coding Seb edited this page Jun 13, 2022 · 10 revisions

Primary types

ExpressionEvaluator manage the following list of C# primary types

  • object
  • string
  • bool/bool?
  • byte/byte?
  • char/char?
  • decimal/decimal?
  • double/double?
  • short/short?
  • int/int?
  • long/long?
  • sbyte/sbyte?
  • float/float?
  • ushort/ushort?
  • uint/uint?
  • ulong/ulong?
  • void

Add the ? for nullable types

Assemblies, Namespaces and types

To resolve types and namespaces ExpressionEvaluator search in in assemblies loaded in the evaluator.Assemblies list. By default this list Contains all loaded assemblies in the current AppDomain when the constructor of ExpressionEvaluator is called. You can easily Clear, Add or Remove assemblies on this list.

By default the following list of namespaces are available :

  • System
  • System.Linq
  • System.IO
  • System.Text
  • System.Text.RegularExpressions
  • System.ComponentModel
  • System.Dynamic
  • System.Collections
  • System.Collections.Generic
  • System.Collections.Specialized
  • System.Globalization

You can extend or reduce this list :

ExpressionEvaluator evaluator = new ExpressionEvaluator();
evaluator.Namespaces.Add(namespace);
evaluator.Namespaces.Remove(namespaceToRemove);

All types defined in these namespaces are accessibles.

You can also add a specific type :

evaluator.Types.Add(typeof(MyClass));

Block the access of some types

If you want to block the access of some types (for security reasons for example). You can add the type to block to the TypesToBlock list.

evaluator.TypesToBlock.Add(typeof(MyCriticalClass));

Inline namespaces

From version 1.3.2.0 You can also write a the namespace directly inline before the name of the class you want to use. And this as soon as the corresponding assembly is referenced in evaluator.Assemblies.

doc = new System.Xml.XmlDocument();
url = $"https://www.google.com/search?q={System.Net.WebUtility.UrlEncode("test of request with url encode() ?\")}"

Remark : Inline namespace are useful because it give access to a lot of things without the need to add all namespaces to the evaluator.Namespaces list.
But it is also a lot slower to execute than the evaluator.Namespaces way in the case of static access.
Depending on how you use ExpressionEvaluator it can also lead to security issues. So if you need more perfs prefer evaluator.Namespaces

To manage security issues of this functionality see the option OptionInlineNamespacesEvaluationRule (available from version 1.4.38.0). If you have an older version see OptionInlineNamespacesEvaluationActive

Table Of Content

Clone this wiki locally