static void Main(string[] args) { Console.WriteLine("Hello World!"); //using default constructor //object obj = MoodAnalyserFactory.CreateMoodAnalyse("EmotionAnalyser.MoodAnalyser", "MoodAnalyser"); //this happy messsage parameter will be passed to message in parameterised constructor object obj = MoodAnalyserFactory.CreateMoodAnalyserParameterizedObject("EmotionAnalyser.MoodAnalyser", "MoodAnalyser", "Happy"); MoodAnalyser mood = (MoodAnalyser)obj; //Converting object to MoodAnalyser class object so that we can call using it Console.WriteLine(mood); }
/// <summary> /// Dynamically setting field variable using Reflection /// </summary> /// <param name="message">The message.</param> /// <param name="fieldName">Name of the field.</param> /// <returns></returns> /// <exception cref="MoodAnalyserCustomException"> /// message should be null /// or /// no field found /// </exception> public static string SetField(string message, string fieldName) { try { MoodAnalyser moodAnalyser = new MoodAnalyser(); Type type = typeof(MoodAnalyser); FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); if (message == null) { throw new MoodAnalyserCustomException(MoodAnalyserCustomException.ExceptionType.NO_SUCH_FIELD, "message should be null"); } field.SetValue(moodAnalyser, message); return(moodAnalyser.message); } catch (NullReferenceException) { throw new MoodAnalyserCustomException(MoodAnalyserCustomException.ExceptionType.NO_SUCH_FIELD, "no field found"); } }