本文整理汇总了C#中Microsoft.CodeAnalysis.Scripting.ScriptOptions.AddNamespaces方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptOptions.AddNamespaces方法的具体用法?C# ScriptOptions.AddNamespaces怎么用?C# ScriptOptions.AddNamespaces使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Scripting.ScriptOptions
的用法示例。
在下文中一共展示了ScriptOptions.AddNamespaces方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession)
{
if (scriptPackSession == null)
{
throw new ArgumentNullException("scriptPackSession");
}
if (references == null)
{
throw new ArgumentNullException("references");
}
_log.Debug("Starting to create execution components");
_log.Debug("Creating script host");
var executionReferences = new AssemblyReferences(references.Assemblies, references.Paths);
executionReferences.Union(scriptPackSession.References);
ScriptResult scriptResult;
SessionState<ScriptState> sessionState;
var isFirstExecution = !scriptPackSession.State.ContainsKey(SessionKey);
if (isFirstExecution)
{
var host = _scriptHostFactory.CreateScriptHost(
new ScriptPackManager(scriptPackSession.Contexts), scriptArgs);
ScriptLibraryWrapper.SetHost(host);
_log.Debug("Creating session");
var hostType = host.GetType();
ScriptOptions = ScriptOptions.AddReferences(hostType.Assembly);
var allNamespaces = namespaces.Union(scriptPackSession.Namespaces).Distinct();
foreach (var reference in executionReferences.Paths)
{
_log.DebugFormat("Adding reference to {0}", reference);
ScriptOptions = ScriptOptions.AddReferences(reference);
}
foreach (var assembly in executionReferences.Assemblies)
{
_log.DebugFormat("Adding reference to {0}", assembly.FullName);
ScriptOptions = ScriptOptions.AddReferences(assembly);
}
foreach (var @namespace in allNamespaces)
{
_log.DebugFormat("Importing namespace {0}", @namespace);
ScriptOptions = ScriptOptions.AddNamespaces(@namespace);
}
sessionState = new SessionState<ScriptState> { References = executionReferences, Namespaces = new HashSet<string>(allNamespaces) };
scriptPackSession.State[SessionKey] = sessionState;
scriptResult = Execute(code, host, sessionState);
}
else
{
_log.Debug("Reusing existing session");
sessionState = (SessionState<ScriptState>)scriptPackSession.State[SessionKey];
if (sessionState.References == null)
{
sessionState.References = new AssemblyReferences();
}
if (sessionState.Namespaces == null)
{
sessionState.Namespaces = new HashSet<string>();
}
var newReferences = executionReferences.Except(sessionState.References);
foreach (var reference in newReferences.Paths)
{
_log.DebugFormat("Adding reference to {0}", reference);
ScriptOptions = ScriptOptions.AddReferences(reference);
sessionState.References = sessionState.References.Union(new[] { reference });
}
foreach (var assembly in newReferences.Assemblies)
{
_log.DebugFormat("Adding reference to {0}", assembly.FullName);
ScriptOptions = ScriptOptions.AddReferences(assembly);
sessionState.References = sessionState.References.Union(new[] { assembly });
}
var newNamespaces = namespaces.Except(sessionState.Namespaces);
foreach (var @namespace in newNamespaces)
{
_log.DebugFormat("Importing namespace {0}", @namespace);
ScriptOptions = ScriptOptions.AddNamespaces(@namespace);
sessionState.Namespaces.Add(@namespace);
}
//.........这里部分代码省略.........