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

C#

 
GeneralRe: Flickering Pin
kmuthuk12-Jan-07 9:16
kmuthuk12-Jan-07 9:16 
AnswerRe: Flickering Pin
Dave Kreskowiak12-Jan-07 9:46
mveDave Kreskowiak12-Jan-07 9:46 
QuestionHow can I retrieve a list of available ports on a specified ip? Pin
wxShayan12-Jan-07 5:03
wxShayan12-Jan-07 5:03 
AnswerRe: How can I retrieve a list of available ports on a specified ip? Pin
led mike12-Jan-07 5:16
led mike12-Jan-07 5:16 
GeneralRe: How can I retrieve a list of available ports on a specified ip? Pin
wxShayan12-Jan-07 5:23
wxShayan12-Jan-07 5:23 
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 
I have the same thread in the MSDN forums, but sadly no one has replied so far. I hope that someone can help me out here Smile | :)

Upon invoking a Delegate created from a DynamicMethod, I receive the following error "Common Language Runtime detected an invalid program.".

It's a bit hard to shape the problem verbally, but I'll try my best, since I would need to post a lot of source code - however, here's my got at it:

I have a Method Manager class. This class allows me to story a collection of MethodInfo objects into a Dictionary object, whose key is a Type.

class MethodInfoCollection: List<MethodInfo> { ... }<br />
<br />
Dictionary<Type, MethodInfoCollection> m_Methods; ...etc


Let's say I'd want to call a specific method for each Key of this m_Methods Dictionary. I would iterate into each MethodInfoCollection and then into each MethodInfo object, checking if the names matche, and then executing via Invoke(); . These MethodInfo objects are coming from different objects instances.

CallMethod("Update"); // this calls every "Update" method found in the m_Methods collection.

Since MethodInfo.Invoke() is way much too slow for my needs, I thought I could come up with another method, that is using DynamicMethods.

I already have a List<string> (m_InterfaceMethods) containing all the possible method names (like "Update" in the above CallMethod call).

My intent is to create a lookup table of Delegates. So, it would be a

delegate void SpecificDelegate();<br />
<br />
Dictionary<string, SpecificDelegate> m_MSILMethods;


- for every known method name, there will be a dynamically created method which in turn will call all the methods - whose names are matching the string Key. I hope this is clear enough.

<code>public static void CallMethod(string MethodName)
{

   m_MSILMethods[MethodName].Invoke();
   // m_MSILMethods[MethodName].Invoke(); should act like this:
   // [pseudocode]
   // Call ObjectA.MethodName;
   // Call ObjectB.MethodName;
   // Call ObjectC.MethodName;
   // ...and so on. instead of having a slow MethodInfo.Invoke() call in a foreach loop each time.

}
</code>


How to create a DynamicMethod which will invoke in its IL a series of methods? Those methods are stored in a MethodInfo class. Which instructions do I require for it to work?
MSIL Method Generator:
<code>DynamicMethod m_MSILMethod;
ILGenerator m_ILGenerator;

foreach (string m_InterfaceMethod in m_InterfaceMethods)
{

[...].
m_MSILMethod = new DynamicMethod(String.Format("Managed{0}", m_InterfaceMethod), null, null, typeof(MyManager));

m_ILGenerator = m_MSILMethod.GetILGenerator();

   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_0); // the method is not static.
            m_ILGenerator.Emit(OpCodes.Call, m_Method); // the actual call to the method
         }

      }
   }

m_ILGenerator.Emit(OpCodes.Ldnull);
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)));
}</code>


Looks like the DynamicMethod generator is not working, perhaps a flaw in the MSIL emitted.

It all boils down to this question, perhaps: How to create a DynamicMethod which will invoke in its IL a series of methods? Those methods are stored as MethodInfos. Which instructions do I require for it to work?

Thanks in advance for your time and any help is much appreciated.
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 
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 

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.