I think your question needs to be divided into two, i.e. should I use C++ or C# and, win32 or .NET. Remember C++ (or more correctly C++/CLI) is a full .NET language. Although most examples on the net are now C#, MS has never said C++ is on the way out. However that can't be said for win32 (over .NET), so may be you should also consider C++ and .NET.
Peter JC
Posts
-
Which platform? -
Dynamic event delegate and the 'this' pointerI create dynamic event delegates (see code at end of question), which call a common method. When an event is fired, my dynamic method is called and in turn calls the common method. My problem is that when in the common method the 'this' pointer is meaningless (as if it thinks its a static), so I can't access members of my class. I have a temporary work around where I build in the members to the dynamic method and pass them to the common method! however it still breaks in the debugger and I have to but gives the desired result. There is something not right with my dynamic method and if anyone can put me on the right track then that would be greatly appreciated. Thanks Peter. void EventHandler::CreateDynamicEventHandler() { System::Type ^ eventClass = m_eventSource->GetTarget()->GetType(); // Get event info and the type of delegate. array^ args; EventInfo ^ eventInfo = eventClass->GetEvent(m_eventName); System::Type ^ tDelegate = eventInfo->EventHandlerType; MethodInfo^ invoke = tDelegate->GetMethod( "Invoke" ); // The generated delegate will call a standard method (CommonEventHandler) // to pass the events data onto Jade. Get the method info for that method. array^ commonArgs = {System::String::typeid, array::typeid}; MethodInfo ^ methodInfo = EventHandler::typeid->GetMethod("CommonEventHandler", commonArgs); // Create dynamic method array ^ paramTypes = GetDelegateParameterTypes(tDelegate); DynamicMethod ^ dynamicMethod = gcnew DynamicMethod("Dyna", nullptr, paramTypes, ::EventHandler::typeid); // Generate method body ILGenerator ^ ilGen = dynamicMethod->GetILGenerator(); // Create a local variable 'args' int paramCount = paramTypes->Length; LocalBuilder ^ localObj = ilGen->DeclareLocal(array::typeid); // create object array of proper length ilGen->Emit(OpCodes::Ldc_I4, paramCount); ilGen->Emit(OpCodes::Newarr, System::Object::typeid); ilGen->Emit(OpCodes::Stloc_0); // Now put all arguments in the object array for (System::Int32 i= 0; iEmit(OpCodes::Ldloc_0); // Local variable - the array ilGen->Emit(OpCodes::Ldc_I4, i); // Index into array ilGen->Emit(OpCodes::Ldarg_S, b); // Value to save is in bth argument to this method if (paramTypes[i]->IsValueType) ilGen->Emit(OpCodes::Box, paramTypes[i]); // Box value types