public void Prepare(bool buildDefaultObject = true) { if (state == State.Prepared) { return; } Compile(); PrepareSeparateAppDomainIfNeeded(); lock (this) { if (state == State.Prepared) { return; } if (buildDefaultObject) { holderObjectAccesor = (ICSharpScriptObjectAccessor)BuildObject(); if (holderObjectAccesor == null) { throw new NullReferenceException("Default host object is null"); } SetObjectsInScope(holderObjectAccesor); } state = State.Prepared; } }
private void SetHostOjectField(ICSharpScriptObjectAccessor scriptObject, string fieldName, object obj) { if (scriptObject == null) { return; } while (true) { try { if (!executeInSeparateAppDomain) { scriptObject.SetField(fieldName, obj); } else { scriptObject.SetField(fieldName, ObjectAddress.GetAddress(obj)); } break; } catch (NotSupportedException) { // capture the exception and try again. This happens because there was a GC call between // the time we obtained the raw address of obj and the call to SetField() } } }
private void SetObjectsInScope(ICSharpScriptObjectAccessor scriptObject) { lock (this) foreach (var obj in objectsInScope) { SetHostOjectField(scriptObject, obj.Key, obj.Value); } }
private void Invalidate() { holderObjectAccesor = null; programText = ""; TryUnloadAppDomain(); TryRemoveTemporaryAssembly(); prg = null; appDomain = null; state = State.NotCompiled; }