script "inheriting" another class

35 views
Skip to first unread message

Gabriel Battochio

unread,
Jan 13, 2012, 11:00:42 PM1/13/12
to CS-Script
I have a class named Foo for example. It has a public variable named
foo_string.
I need to run a script inside that foo class, and in the script do
something like:

public void Show() { Console.WriteLine(this.foo_string); }

how can I do that?

Oleg Shilo

unread,
Jan 14, 2012, 5:25:05 AM1/14/12
to cs-s...@googlegroups.com

You should treat C# script routine as any other C# routine. After all C#Script is just another assembly.   

Thus the answer is:

public void Show(string message) { Console.WriteLine(message); }
...

script.Show(this.foo_string );

Gabriel Battochio

unread,
Jan 14, 2012, 4:20:20 PM1/14/12
to CS-Script
Thanks for your answer. But I wouldnt like to pass foo_string as a
method argument. is it possible?
Message has been deleted

Desert Serenity

unread,
Jan 14, 2012, 5:00:00 PM1/14/12
to cs-s...@googlegroups.com

No. Doing so would require the class to span multiple assemblies which is not allowed, and would not be a best practice.

You should create a separate class in the script and interface with it to perform whatever function you purpose. 'foo_string' should be passed to it as an argument in the method or constructor. If it really does not make sense to pass 'foo_string' to the script class, then you might reconsider your implementation design.

Oleg Shilo

unread,
Jan 14, 2012, 6:31:19 PM1/14/12
to CS-Script
You can still pass the Foo class instance:
public void Show(Foo foo) { Console.WriteLine(foo.foo_string); }
Providing Foo is public.

However Desert is wright. You may want to reconsider your API design
if the only thing that Show() does with the Foo instance is accessing
the foo_string member.

public void Show() { Console.WriteLine(this.foo_string); }
Is illegal in C# as "this" means script class not Foo.

But I agree with Desert your design

Gabriel Battochio

unread,
Jan 14, 2012, 6:59:16 PM1/14/12
to CS-Script
I see. I'll reconsider my emplementation design.
What about security? how can I restrict some namespaces in my script?
supposing it is possible.

Desert Serenity

unread,
Jan 14, 2012, 8:43:13 PM1/14/12
to cs-s...@googlegroups.com

That depends on what you are trying to protect against, or what restriction you want to create. For one, access modifiers can be applied to individual classes, but not to namespaces, directly. CS-Script does not currently provide any functionality of its own for this.

Oleg Shilo

unread,
Jan 15, 2012, 2:35:50 AM1/15/12
to CS-Script
The best way to approach the task is to treat the script your host
application is executing as an external assembly (in fact it is
exactly what the script at runtime is).

You load the assembly and want this assembly to execute some routine,
probably access some Host application variables and do all this in a
security respectable manner. All this is a typical .NET scenario and
it is addressed by the CLR sandboxing (and arguably by CAS as well).
That is why CS-Script does not address the runtime security by itself
but relyes on the solution provided by CLR.

Have a look at the [cs-script]\Samples\Sandboxing samples for the
idea....

On Jan 15, 12:43 pm, Desert Serenity
Reply all
Reply to author
Forward
0 new messages