// Invoke method using reflection public static string InvokeAnalyseMood(string methodName, string message) { try { // First an instance of MoodAnalyzer is created with the help of reflection MoodAnalyzer moodAnalyzer = (MoodAnalyzer)MoodAnalyzerReflection.CreateMoodAnalyzerObject("MoodAnalyzerProblem.MoodAnalyzer", "MoodAnalyzer", message); // Meta information of MoodAnalyzer var moodAnalyzerType = typeof(MoodAnalyzer); // Meta information of methodName provided in arguments while calling this function //GetMethod Returns an object that represents the public method with the specified name, if found; otherwise, null. var analyseMoodMethod = moodAnalyzerType.GetMethod(methodName); // Now invoke method using meta information of method, need an instance of class and parameters as arguments var mood = analyseMoodMethod.Invoke(moodAnalyzer, null); return(mood.ToString()); } catch (NullReferenceException) { // Get methods returns null incase no method is found with given methodName // Invoke method will throw exception throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NO_SUCH_METHOD, "No such method exist!"); } catch (TargetInvocationException ex) { // When message is null or empty this exception is thrown throw ex.InnerException; } }
static void Main(string[] args) { try { ///Change Field With Reflection MoodAnalyzer moodAnalyzer = new MoodAnalyzer("I am in sad mood!"); //This instance can also be created with Reflection Console.WriteLine($"Initial Mood was {moodAnalyzer.AnalyseMood()}!"); string newMessage = "I am in happy mood!"; moodAnalyzer = (MoodAnalyzer)MoodAnalyzerReflection.SetField(moodAnalyzer, "message", newMessage); Console.WriteLine($"New Mood is {moodAnalyzer.AnalyseMood()}!"); } catch (MoodAnalyzerException ex) { Console.WriteLine(ex.Message); } }
public static object SetField(MoodAnalyzer moodAnalyzer, string fieldName, string newMessage) { try { // Meta information of MoodAnalyzer var moodAnalyzerType = typeof(MoodAnalyzer); // Meta information of messageField //GetField Returns an object that represents the field with the specified name, if found; otherwise, null. var messageField = moodAnalyzerType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); messageField.SetValue(moodAnalyzer, newMessage); return(moodAnalyzer); } catch (NullReferenceException) { // GetField returns null incase no field is found with given fieldName // SetValue method will throw exception throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NO_SUCH_METHOD, "No such method exist!"); } }
/// <summary> /// creating Sets the field. /// </summary> /// <param name="message">The message contain happy.</param> /// <param name="fieldName">Name of the field that is message.</param> /// <returns>message of mood analyzer class</returns> /// <exception cref="MoodAnalyzerException"> /// Message should not be null /// or /// Field is not found /// </exception> public static string SetField(string message, string fieldName) { try { MoodAnalyzer moodAnalyzer = new MoodAnalyzer(); Type type = typeof(MoodAnalyzer); FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); if (message == null) { throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NO_SUCH_FIELD, "Message should not be null"); } field.SetValue(moodAnalyzer, message); return(moodAnalyzer.message); } catch (NullReferenceException) { throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NO_SUCH_FIELD, "Field is not found"); } }
/* UC7:- Use Reflection to change mood dynamicall * - User Reflector to Modify mood dynamically */ //public static string SetField(string message, string fieldName) //{ // MoodAnalyzerFactory Fact = new MoodAnalyzerFactory(); // MoodAnalyzer obj = (MoodAnalyzer)Fact.CreateMoodAnalyzerObject("MoodAnalyzerProblem.MoodAnalyzer", "MoodAnalyzer"); // Type type = typeof(MoodAnalyzer); // FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); // if (field!= null) // { // if (message==null) // { // throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NULL_MESSAGE, "Message should not be null"); // } // field.SetValue(obj, message); // return obj.message; // } // throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.FIELD_NULL, "FieldName should not be null"); //} /* UC7:- Use Reflection to change mood dynamically * - User Reflector to Modify mood dynamicall */ public static string SetField(string message, string fieldName) { try { if (message == null) { throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NULL, "Mood should not be NULL"); } MoodAnalyzer obj = new MoodAnalyzer(); Type type = Type.GetType("MoodAnalyzerProblem.MoodAnalyzer"); FieldInfo field = type.GetField(fieldName); field.SetValue(obj, message); return(obj.message); } catch (NullReferenceException) { throw new MoodAnalyzerException(MoodAnalyzerException.ExceptionType.NO_SUCH_FIELD, "No Such Field"); } }