0

My question is: Can I define a static method "meth1" in a static class "classB" that, when called from "classA", searches for a specific field (in "classA", not in the class in which is defined)? I try to explain better: I need to do something like this:

public class classA
{
  string someText;
  int anInt;
  bool trueOrFalse;

  public classA()
  {
    ...
    ...
    var variable = classB.meth1("variableName");
    ...
    ...
  }
}

public static classB
{
  public static object meth1(string name)
  {
  ...
  ... //use "name" to find the variable with that name in the class from which "meth1" is called.
  ...
  }
}

That because I have to read a backup of "last run values" of variables contained in a .txt file, written line by line as "variable name = value". So I read the .txt, create an hashtable "backupHashtable" which contains ("variable name";"value"), and then I want to search variables by string "variable name" and reset them to "value". If someone of you knows a better procedure I'm listening. Maybe the use of a Dictionary?

Thank you!

UPDATING

Ok, now I have a clearer idea of what I want to do: I want to implement a class "ClassB", separate from my main class "classA". In this new class I would have a "meth1" method which, running in a separate thread, saves every 10 seconds (for example) the state of some variables belonging to "classA". To communicate the "meth1" what are the variables that he has to save, I want to use a list containing the names (in the format "string", that's what I thought, but I guess it's not the only way) of these variables.

If you're wondering why I want to do this in a separate thread the answer is this: my application performs some recognition operation of some objects in live stream images from multiple cameras and then ancillary operations must be isolated as much as possible from the main code to increase the speed of execution.

Now, perhaps, it is more understandable what I said before.

Andrea
  • 17
  • 1
  • 7
  • Are you trying to implement a serializer? Why not use an existing one? – Maarten Nov 16 '16 at 15:18
  • 2
    I'm confused - why do you need `meth1` to _return_ a value that's in the class that you're calling it from? Can you give more sample code that shows what you're trying to do in either class? – D Stanley Nov 16 '16 at 15:43
  • @Maarten Thank you. It will come in handy. But, as I understand it, serialization is only a tool to save the current state of the application, but in this case I still need to have some variables, of various types, grouped in one container. – Andrea Nov 16 '16 at 16:10
  • @DStanley imagine that I need the value of a large number of variables in my class, variables of which I know only the string that defines the name, and I do not want the "Service code" that allows me to associate the string to the variable to appear in the code of my class. Maybe today I thought about it too much. Better think again tomorrow. – Andrea Nov 16 '16 at 16:10
  • @Andrea so you're essentially wrapping the reflection code in a static class method? That's fine but I agree there may be better structures like `Dictionary` that make it easier. – D Stanley Nov 16 '16 at 16:12

1 Answers1

1

Yes, but you also need to pass a reference to the instance of A. Then use reflection to get or set the property.

public static void Meth1(object obj, string propertyName)
{
    var prop = obj.GetType().GetProperty(propertyName);
    var value = prop.GetValue(obj);
    ...
}

If I were to get values from a textfile into a class, I think I'd load them in a dictionary first, and then set all properties one by one. (Maybe unless there are, say, hundreds of properties). When using reflection, there is a chance that the textfile contains the name of a property you don't want to be changed from outside.

object value;
if (dict.TryGetValue("someProperty", out value)) { a.SomeProperty = value; }
Berend
  • 780
  • 7
  • 19