HighTechTalks DotNet Forums  

Passing reference object to dynamic executing code

Dotnet Scripting microsoft.public.dotnet.scripting


Discuss Passing reference object to dynamic executing code in the Dotnet Scripting forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jeronimo Bertran
 
Posts: n/a

Default Passing reference object to dynamic executing code - 09-08-2006 , 08:14 PM






I have created an application that allows dynamically executing code. A
lot of the concepts are based on Rick Strahl's article in
http://www.west-wind.com/presentatio...ynamicCode.htm which
creates code in alternate AppDomains to eliminate the memory leak caused
by loading assemblies on the main AppDomain and not being able to unload
them.

Everything works fine, except that I am having trouble passing objects
by reference to my dynamic code.

Here is a description of the problem:

The first thing I tried is to keep an instance of an object inside the
script that could be requested by the calling application. I created a
class called ScriptingObject in a dll that was included both the calling
application and the dynamic code.


public class ScriptingObject

{
private string current = "";
public string Current
{
get { return current; }
}

public void SetCurrent(string val)
{
current = val;
}
}



In the dynamicClass (TheClass) I keep a member variable of that type.

The full source for the script looks like the following:

using System;
using System.Reflection;
using Scripting;
using ScriptingObject;

namespace DynamicScript
{
public class TheScript : System.MarshalByRefObject,
Scripting.IRemoteInterface
{
// variable that keeps the instance of the scripting object
private ScriptingObject sys;

// Property that returns the scripting object
public virtual ScriptingObject. SysObject
{
get {return this.sys;}
}

// Constructor for the class
public TheScript()
{
// Create an instance of the scripting object
sys = new ScriptingObject();
}

public virtual object Invoke(string method, object[] parameters)
{
return this.GetType().InvokeMember(method,
System.Reflection.BindingFlags.InvokeMethod, null, this, parameters);
}

public virtual string Method1()
{
sys.SelectCurrent(sys.Current + "X");
return sys.Current;
}

public virtual string Method2(ScriptingObject obj)
{
obj.SelectCurrent(obj.Current + "X");
return obj.Current;
}

}

}

In my application, before calling Method1 I tried retrieving the sys
object using the property. When I try to execute:



ScriptingObject obj = (ScriptingObject) scripting.CallMethod
("get_Current", null);


I receive an exception stating that ScriptingObject must be marked
serializable. But if I do declare it serializable then a copy is
returned and not a reference, hence the following:



ScriptingObject obj = (ScriptingObject) scripting.CallMethod
("get_Current", null);
obj.SelectCurrent("Y");
string result = (string) scripting.CallMethod("Method1", null);

returns "X" and not "YX" as expected if both were dealing with the same
object.

Similarly, the following:

ScriptingObject newObject = new ScriptingObject();
newObject.Current = "Y";
string result = (string) scripting.CallMethod("Method2", new object[] {
newObject});

returns "YX", but if I inspect newObject, it still holds the value "Y".

Thanks,

Jeronimo Bertran




Reply With Quote
  #2  
Old   
Luke Zhang [MSFT]
 
Posts: n/a

Default RE: Passing reference object to dynamic executing code - 09-11-2006 , 02:09 AM






Hello,

I suspect the reason is that the ScriptingObject is in different Appdomain
with your application, so that it was not referenced. Will it got fixed if
you inherits System.MarshalByRefObject in class ScriptingObject?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.




Reply With Quote
  #3  
Old   
Luke Zhang [MSFT]
 
Posts: n/a

Default RE: Passing reference object to dynamic executing code - 09-13-2006 , 05:20 AM



Hello,

Just check if my previous suggestion make sense or you have other concern
on the issue. If so, please feel free to let us know.

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.




Reply With Quote
  #4  
Old   
Jeronimo Bertran
 
Posts: n/a

Default RE: Passing reference object to dynamic executing code - 09-28-2006 , 08:32 PM



Luke,

Yes, I was able to fix the problem...

Thanks

Reply With Quote
  #5  
Old   
Luke Zhang [MSFT]
 
Posts: n/a

Default RE: Passing reference object to dynamic executing code - 09-29-2006 , 02:29 AM



Thank you for the update. I am glad to hear that

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.