Archived

This topic is now archived and is closed to further replies.

Question for anyone using C# for scripting - or atleast Nypyren

This topic is 5075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to limit what namespaces/classes a script writer can have access to within a C# script? I know that it automatically references mscorlib.dll, which gives the script access to the System namespace but also the Reflection, IO, Microsoft.Win32, and some others. To me this seems a little strange. Kory Spansel "The pioneers of a warless world are the youth who refuse military service" - Albert Einstein

Share this post


Link to post
Share on other sites
If you''re worried about security you can limit your scripts from executing certain methods. Just give them partial trust instead of full trust. You can read up on this in MSDN.

Share this post


Link to post
Share on other sites
Thanks for the reply. I realized like 5-minutes after I posted that I should check out the System.Security namespace, so I''ve been doing some research with that, and I think it''s exactly what I''m looking for. Thanks.

Kory Spansel

"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein

Share this post


Link to post
Share on other sites
Okay, it turns out this is a little more complicated than I originally thought. I've read a bunch about how to set/get various permission for an assembly but all of the documentation refers to setting the permissions for the current assembly, which will not work for me. Then I tried to setup an AppDomain with the correct permissions, which worked ok, but I then ran into the problem of loading the assembly into that domain. I'm not sure how to load the assembly into the domain, because I'm dynamically compiling the assembly.

I keep getting a FileLoadException: The located assembly's manifest definition with name 'test_assembly' does not match the assembly reference.

Here's what I have:

I have already setup the AppDomain (scriptDomain) and CompileSourceFile is just a compiling helper method.


compilerParams.GenerateInMemory = true;
compilerParams.GenerateExecutable = false;
compilerParams.OutputAssembly = "test_assembly"; // Script Name



Assembly compiledAssembly = CompileSourceFile( codeCompiler, compilerParams, scriptName );


if( compiledAssembly != null )
{

scriptDomain.Load( compiledAssembly.GetName( ) );


object scriptObject = compiledAssembly.CreateInstance( "ScriptTest1" );


MethodInfo scriptMethod = scriptObject.GetType( ).GetMethod( "ScriptTest" );


try
{
scriptMethod.Invoke( scriptObject, null );
}
catch( SecurityException ex )
{
Console.WriteLine( ex.Message );
}
}


"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein

Damn Formatting!?!

[edited by - kspansel on January 23, 2004 5:48:51 AM]

[edited by - kspansel on January 23, 2004 5:49:57 AM]

Share this post


Link to post
Share on other sites