Creating Delegates in OE 10.2B

alina_moise24

New Member
I am trying to dynamically call a method, an alternative to InvokeMethod which can be too slow.
Do you have any suggestion on how can I do this?

When I run the below code I get following exception: Type must derive from Delegate.
Any idea how can I get the delegate type?

CLASS Dict:
DEFINE PROTECTED VARIABLE iObjArray AS System.Array NO-UNDO.
DEFINE PROTECTED VARIABLE iType AS System.Type NO-UNDO.
DEFINE PROTECTED VARIABLE iDict AS System.Object NO-UNDO.
DEFINE PROTECTED VARIABLE iMethodInfo AS System.Reflection.MethodInfo NO-UNDO.
DEFINE PROTECTED VARIABLE iDelType AS System.Type NO-UNDO.
DEFINE PROTECTED VARIABLE iDelegate AS System.Delegate NO-UNDO.

CONSTRUCTOR Dict() :
iType = TypeHelper:GetType("System.Collections.Generic.Dictionary`2[System.String,System.Object]").
iDict = Activator:CreateInstance(iType).
iObjArray = System.Array:CreateInstance(TypeHelper:GetType("System.Object"), 1).
END CONSTRUCTOR.

METHOD PUBLIC System.Object GetValue(INPUT chKey AS CHARACTER):
iObjArray:SetValue(chKey, 0).
/*RETURN inoType:InvokeMember("Item", BindingFlags:GetProperty, ?, iDict, iObjArray).*/
iMethodInfo = iType:GetMethod("Item", BindingFlags:GetProperty).
iDelType = iMethodInfo:GetType().
iDelegate = System.Delegate:CreateDelegate(iDelType, iDict, iMethodInfo).
RETURN iDelegate:DynamicInvoke(iObjArray).
END METHOD.

DESTRUCTOR PUBLIC Dict() :
DELETE OBJECT iObjArray.
END DESTRUCTOR.
END CLASS.

Does anyone know how can I create an object using reflections and make it fast?
Thanks in advance
 
Reflection support in ABL is limited. I wouldn't expect to do much with it.

But, what are you actually trying to do? I feel that reflection should mostly be confined to framework code and that using it in application code is usually a sign of trying to do something "clever" when something simple would actually be preferred for being able to understand and maintain the code.
 
I need a dictionary to store information in Key/Value pairs, where RECID is the Key and a .NET object is the Value.
I want to access the .NET objects based on certain keys, also to create a .NET object with a given RECID.
InvokeMember is often very slow and I wanted a better way of calling a member.
Unfortunately, CreateDelegate() requires the Type object of the delegate that I want to create.
Therefore, I tried to use refection and to create a custom delegate type on the fly. But, I failed.
So, back to the purpose...Another way to create and handle the dictionary is to use a SortedList() instead of reflection, which implements the IDictionary interface.
Or, to create a class which implements this interface.
I don't know which mechanism is faster but I suppose that any is better than a late-binding.
Is there another way to create a dictionary in OE 10.2B based on .NET framework 2.0.
Thank you
 
If you are looking for plain ABL implementations of HashMap (Dictionary) then there are a few hanging around, Dr. Thomas can point you to the right direction on OEHive... don't know if any supports primitive types (recid or character, it's not clear what you want to use as a key), the one I've released on sf.net only works with objects so the primitive need to be wrapped (as a String for instance) and it follows the Java implementation (HashMap) as opposed to the .net one (IDictionary).
 
Back
Top