Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public class ReflectionContext : IContext
  2. {
  3. public ReflectionContext(object targetObject)
  4. {
  5. _targetObject = targetObject;
  6. }
  7.  
  8. object _targetObject;
  9.  
  10. public double ResolveVariable(string name)
  11. {
  12. // Find property
  13. var pi = _targetObject.GetType().GetProperty(name);
  14. if (pi == null)
  15. throw new InvalidDataException($"Unknown variable: '{name}'");
  16.  
  17. // Call the property
  18. return (double)pi.GetValue(_targetObject);
  19. }
  20.  
  21. public double CallFunction(string name, double[] arguments)
  22. {
  23. // Find method
  24. var mi = _targetObject.GetType().GetMethod(name);
  25. if (mi == null)
  26. throw new InvalidDataException($"Unknown function: '{name}'");
  27.  
  28. // Convert double array to object array
  29. var argObjs = arguments.Select(x => (object)x).ToArray();
  30.  
  31. // Call the method
  32. return (double)mi.Invoke(_targetObject, argObjs);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement