static void Main(string[] args) { try { Console.WriteLine("Enter the Message"); string message = Console.ReadLine(); MoodAnalyse moodAnalyse = new MoodAnalyse(message); string mood = moodAnalyse.AnalyseMood(); Console.WriteLine(mood); } catch (MoodAnalysisException exception) { Console.WriteLine(exception.Message); } }
/// <summary> /// Function to Set The Field Dynamically using Reflection. /// </summary> /// <param name="moodAnalyseobject"></param> /// <param name="message"></param> public static void SetField(MoodAnalyse moodAnalyseobject, string message, string fieldName) { try { Type type = Type.GetType("MoodAnalyse"); FieldInfo field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); field.SetValue(moodAnalyseobject, message); } catch (System.NullReferenceException exception) { throw new MoodAnalysisException(MoodAnalysisException.ExceptionType.NO_SUCH_FIELD, "No Such Field Found"); } catch (MoodAnalysisException exception) { throw new MoodAnalysisException(MoodAnalysisException.ExceptionType.ENTERED_NULL, "Can Not Set Null To Field"); } }
public static string SetField(string message, string fieldName) { try { MoodAnalyse moodAnalyse = new MoodAnalyse(); Type type = typeof(MoodAnalyse); FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); if (message == null) { throw new MoodAnalyseException(MoodAnalyseException.ExceptionType.NO_SUCH_FIELD, "Message should not be null"); } field.SetValue(moodAnalyse, message); return(moodAnalyse.message); } catch (NullReferenceException) { throw new MoodAnalyseException(MoodAnalyseException.ExceptionType.NO_SUCH_FIELD, "Field is Not Found"); } }