lundi 8 juin 2020

How to Handle Custom Objects Back and Forth (T->Object, Object->T) C#?

I have two .cs files. In one of them I have this class:

...
public  Dictionary<String, Object> Resources = new Dictionary<String, Object>();
...

private static int someRandomMethod(){
    ....
    object[] params = new object[2];
    string somecommand = "X";
    params[0] = somecommand;
    params[1] = Resources;
    string result_output, Resources = (List<Object>)some.GetType("Class2").GetMethod("theActualMethod").Invoke(null, params);

    .....

and in the other one I have two Classes:

public class SomeClass{
...
}

public class Class2{

    public static (String output, Dictionary<String, Object> resources) theActualMethod(String Command, Dictionary<String, Object>  resources_temp){
   this_class_resource = (SomeClass)resources_temp["SomeClass"]; 

   ... process this_class_resource ...
   ... process Command and get output...

   resources_temp["SomeClass"] = this_class_resource; #updated resource back to the dictionary
   #return the updated dictionary
   return (output, resources_temp);
}

So.. the thing is, the first class have a dictionary of different resources and can have any custom object in its key values. These resources are sent to the Invoke method.

The method "theActualMethod" should be able to receive this dictionary of different objects and will only process the object that it had implemented, in case, SomeClass. There might be a lot of different objects in this dictionary.

Then, it will cast the object back to its type ("SomeClass") and process it (the key name is the same as the object type, and no, there won't be two objects of the same type in the dictionary) and add it back to the dictionary and return it with the output string.

The process is like sending the Dictionary to the function with Invoke, getting it back updated and repeating it...

The first call to the Invoke method works fine, but after it is not working anymore...

What is wrong with it? Is it the casting of Object to SomeClass back and forth?





Aucun commentaire:

Enregistrer un commentaire