Click here to Skip to main content
15,888,461 members
Home / Discussions / C#
   

C#

 
Generaldebugging Pin
justintimberlake14-Jan-08 17:22
justintimberlake14-Jan-08 17:22 
GeneralRe: debugging Pin
Skippums14-Jan-08 17:32
Skippums14-Jan-08 17:32 
QuestionDoes a race condition exist in the following code? Pin
Skippums14-Jan-08 14:57
Skippums14-Jan-08 14:57 
GeneralRe: Does a race condition exist in the following code? Pin
S. Senthil Kumar14-Jan-08 18:03
S. Senthil Kumar14-Jan-08 18:03 
GeneralRe: Does a race condition exist in the following code? Pin
Skippums15-Jan-08 4:48
Skippums15-Jan-08 4:48 
GeneralRe: Does a race condition exist in the following code? Pin
PIEBALDconsult15-Jan-08 15:45
mvePIEBALDconsult15-Jan-08 15:45 
GeneralRe: Does a race condition exist in the following code? Pin
Pete O'Hanlon16-Jan-08 1:20
mvePete O'Hanlon16-Jan-08 1:20 
QuestionDynamic Method Delegate [modified] Pin
Gywox14-Jan-08 14:25
Gywox14-Jan-08 14:25 
Hello Everyone!!

I'm trying to add a delegate to a Dictionary<string, object>, hence of running it through IronPython (script file).
I have managed this (look at the code) but now I want to do this dynamic. In other words I don't want
to declare a delegate for every method in my class (ExCode. AddItem in Actor).
The problem is the method CreateDelegate and the first argument. I can't bind a dynamic type representing the type of delegate to create. I have not found a solution for this on CodeProject but where is greatly on the subject.

Dynamic Code Generation vs Reflection By Herbrandson
Fast late-bound invocation through DynamicMethod delegates By Alessandro Febretti
A General Fast Method Invoker By Luyan

This is the code:
<br />
using System.Reflection;<br />
using IronPython.Hosting;<br />
<br />
public class Actor<br />
{<br />
    private string objectID;<br />
    private int count;<br />
<br />
    public void AddItem(string objectID, int count)<br />
    {<br />
         Console.WriteLine(string.Format("{0}, {1}", objectID, count));<br />
         this.objectID = objectID;<br />
         this.count = count;<br />
    }<br />
}<br />
<br />
public class ActorScript<br />
{<br />
    protected delegate void AddItem(string objectID, int count);<br />
    protected Dictionary<string, object> locals = new Dictionary<string,object>();<br />
    protected Actor actor;<br />
<br />
    ActorScript(Actor actor)<br />
    {<br />
        this.actor = actor;<br />
        Initiate();<br />
    }<br />
<br />
    private void Initiate()<br />
    {<br />
         MethodInfo methodInfo = typeof(Actor).GetMethod("AddItem", BindingFlags.Public | BindingFlags.Instance);<br />
         this.locals.Add("AddItem", Delegate.CreateDelegate(typeof(AddItem), this.actor, methodInfo));<br />
<br />
        //string[] arr = new string[] {"AddItem", "DropItem", "GetItemCount"};<br />
        //for (int i = 0; i < arr.Length; i++)<br />
        //{<br />
            //MethodInfo methodInfo = typeof(Actor).GetMethod(arr[i], BindingFlags.Public | BindingFlags.Instance);<br />
             //this.locals.Add(arr[i], Delegate.CreateDelegate( ??? , this.actor, methodInfo));<br />
        //}<br />
    }<br />
<br />
    public void Run(string fileName)<br />
    {<br />
         PythonEngine engine = new PythonEngine();<br />
         engine.ExecuteFile(fileName, this.engine.DefaultModule, this.locals);<br />
    }<br />
}<br />
<br />
This is the script file:<br />
<br />
ItemScript.py<br />
AddItem("Sword", 2)<br />


Any help would be appreciated.
Best Regards,
Gywox

modified on Tuesday, January 15, 2008 5:13:06 AM

GeneralRe: Dynamic Method Delegate Pin
Mark Churchill14-Jan-08 21:49
Mark Churchill14-Jan-08 21:49 
GeneralRe: Dynamic Method Delegate Pin
Gywox14-Jan-08 23:26
Gywox14-Jan-08 23:26 
GeneralRe: Dynamic Method Delegate Pin
Mark Churchill15-Jan-08 2:00
Mark Churchill15-Jan-08 2:00 
GeneralComponent1 vs Infragistics vs Syncfusion Pin
kozu14-Jan-08 13:03
kozu14-Jan-08 13:03 
GeneralRe: Component1 vs Infragistics vs Syncfusion Pin
Not Active14-Jan-08 14:31
mentorNot Active14-Jan-08 14:31 
GeneralRe: Component1 vs Infragistics vs Syncfusion Pin
J$14-Jan-08 14:44
J$14-Jan-08 14:44 
GeneralRe: Component1 vs Infragistics vs Syncfusion Pin
kozu14-Jan-08 18:15
kozu14-Jan-08 18:15 
GeneralRe: Component1 vs Infragistics vs Syncfusion Pin
Nouman Bhatti14-Jan-08 22:32
Nouman Bhatti14-Jan-08 22:32 
GeneralRe: Component1 vs Infragistics vs Syncfusion Pin
IAM...13-Mar-10 14:01
IAM...13-Mar-10 14:01 
Questionprogramming into smart card Pin
roonierobster14-Jan-08 12:04
roonierobster14-Jan-08 12:04 
QuestionC# .NET and a networked PC with MySQL running Pin
CoolBytes14-Jan-08 11:48
CoolBytes14-Jan-08 11:48 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
DaveyM6914-Jan-08 12:10
professionalDaveyM6914-Jan-08 12:10 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
Not Active14-Jan-08 12:13
mentorNot Active14-Jan-08 12:13 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
CoolBytes14-Jan-08 12:16
CoolBytes14-Jan-08 12:16 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
DaveyM6914-Jan-08 12:34
professionalDaveyM6914-Jan-08 12:34 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
CoolBytes14-Jan-08 12:48
CoolBytes14-Jan-08 12:48 
GeneralRe: C# .NET and a networked PC with MySQL running Pin
Mark Churchill14-Jan-08 13:15
Mark Churchill14-Jan-08 13:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.