Click here to Skip to main content
15,894,540 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I retrieve a list of available ports on a specified ip? Pin
Eric Dahlvang12-Jan-07 6:39
Eric Dahlvang12-Jan-07 6:39 
GeneralRe: How can I retrieve a list of available ports on a specified ip? Pin
wxShayan12-Jan-07 17:00
wxShayan12-Jan-07 17:00 
QuestionLCG issue (DynamicMethod / ILGenerator) Pin
rcollina12-Jan-07 5:03
rcollina12-Jan-07 5:03 
AnswerRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar12-Jan-07 6:42
S. Senthil Kumar12-Jan-07 6:42 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina12-Jan-07 8:49
rcollina12-Jan-07 8:49 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar12-Jan-07 20:31
S. Senthil Kumar12-Jan-07 20:31 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina13-Jan-07 13:45
rcollina13-Jan-07 13:45 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar13-Jan-07 21:07
S. Senthil Kumar13-Jan-07 21:07 
I can't believe I missed it earlier, but the source of your problem is that the dynamically generated method is added as a static method. Being a static method, it does not have a this parameter, so a valid object reference needs to be passed. In my sample code, that would mean that MyDelegate would be modified like this
delegate void MyDelegate(Program p);
. The dynamic method's constructor would look like this
DynamicMethod dynMethod = new DynamicMethod("DynTestMethod", null, new Type[] { typeof(Program) }, typeof(Program)); // Notice typeof(Program) added to the list of parameter types

and the dynamic method invocation would look like this
d.Invoke(p); // p is an instance of Program


At the C# level, the generated method would look like this
class Program
{
   ...
   static void DynMethod(Program p)
   {
      p.TestMethod();
      p.TestMethod2();
   }
}

As you can see, earlier, there was no parameter being passed to the static method at all, which resulted in the weird exception you got.

In your case, the simplest way would be to make m_MSILMethod accept parameters for all types on which methods would be invoked in its body.
1. In the constructor of the dynamic methods, you would need to provide all the types whose methods would be invoked (all elements in the m_TypeExecutionOrder array?)
2. The m_ILGenerator.Emit(OpCodes.LdArg_0) would obviously need to be modified to point to the correct parameter type.
Something like this should work fine
DynamicMethod m_MSILMethod;
ILGenerator m_ILGenerator;

foreach (string m_InterfaceMethod in m_InterfaceMethods)
{

[...].
m_MSILMethod = new DynamicMethod(String.Format("Managed{0}", m_InterfaceMethod), null, m_TypeExecutionOrder.ToArray(), typeof(MyManager)); //assuming m_TypeExecutionOrder is List<Type>

m_ILGenerator = m_MSILMethod.GetILGenerator();

int index = 0;

   foreach (Type m_Type in m_TypeExecutionOrder)
   {

      foreach (MethodInfo m_Method in m_Methods[m_Type])
      {

         if (m_Method.Name == m_InterfaceMethod)
         {
            m_ILGenerator.Emit(OpCodes.Ldarg, index); // note the dynamic changing of index according to the type
            m_ILGenerator.Emit(OpCodes.Call, m_Method); // the actual call to the method
         }

      }

      index++
   }

m_ILGenerator.Emit(OpCodes.Ret);


// Once the delegate is created, Emit() won't add more instructions, so the DynamicMethod is "done".
m_MSILMethods.Add(m_InterfaceMethod, (SpecificDelegate)m_MSILMethod.CreateDelegate(typeof(SpecificDelegate)));


SpecificDelegate's signature would need to be modified to something like
delegate void SpecificDelegate(MyObject obj1, YourObject obj2, othertypes...);


And of course, when calling the method, you would need to provide instances of all those types specified in the parameter list. Something like
MyObject obj1 = new MyObject();
YourObject obj2 = new YourObject();
m_InterfaceMethod.Invoke(obj1, obj2);


If this looks too ugly, you could have a single object exposing properties for getting the required object references and pass that single object to the dynamically generated method. The IL, of course, would change and would be more complex (you would need to generate code for getting object references from properties).

Hope this helps.



GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina14-Jan-07 2:27
rcollina14-Jan-07 2:27 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina15-Jan-07 14:02
rcollina15-Jan-07 14:02 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina17-Jan-07 13:28
rcollina17-Jan-07 13:28 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar17-Jan-07 17:51
S. Senthil Kumar17-Jan-07 17:51 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina20-Jan-07 10:02
rcollina20-Jan-07 10:02 
QuestionFinding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 4:21
efriese112-Jan-07 4:21 
AnswerRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 4:34
Mircea Puiu12-Jan-07 4:34 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 4:53
efriese112-Jan-07 4:53 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:00
Mircea Puiu12-Jan-07 5:00 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:09
efriese112-Jan-07 5:09 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:12
Mircea Puiu12-Jan-07 5:12 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:19
efriese112-Jan-07 5:19 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:20
Mircea Puiu12-Jan-07 5:20 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:28
efriese112-Jan-07 5:28 
AnswerRe: Finding a Sorted DataGridView row in a Dataset Pin
Drew McGhie12-Jan-07 9:55
Drew McGhie12-Jan-07 9:55 
QuestionMake a web browser that work in single window Pin
Par Witch12-Jan-07 4:15
Par Witch12-Jan-07 4:15 
AnswerRe: Make a web browser that work in single window Pin
Mircea Puiu12-Jan-07 4:28
Mircea Puiu12-Jan-07 4:28 

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.