public static string SetFieldUsingReflection(string methodName, string fieldName, string message)
        {
            try
            {
                MethodBase method        = typeof(MoodAnalyser).GetMethod(methodName);
                object     objInstance   = Activator.CreateInstance(typeof(MoodAnalyser), message);
                string[]   str           = { message };
                string     returnMessage = (string)method.Invoke(objInstance, str);

                MoodAnalyser myObject    = new MoodAnalyser();
                FieldInfo    myFieldInfo = typeof(MoodAnalyser).GetField(fieldName,
                                                                         BindingFlags.NonPublic | BindingFlags.Instance);
                // Change the field value using the SetValue method.
                myFieldInfo.SetValue(myObject, "HAPPY");
                // Display the string after applying SetValue to the field.
                string msg = (string)myFieldInfo.GetValue(myObject);
                return(msg);
            }
            catch (NullReferenceException)
            {
                throw new MoodAnalyserException(MoodAnalyserException.ExceptionType.No_Such_Field_Error, "Enter Proper Method Name");
            }
            catch (ArgumentNullException)
            {
                throw new MoodAnalyserException(MoodAnalyserException.ExceptionType.No_Such_Field_Error, "Enter Proper Method Name");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// To compare Two Object and if Equals Return True
        /// </summary>
        /// <param name="moodAnalyserFactory"></param>
        /// <returns></returns>
        public bool equals(Object moodAnalyserFactory)
        {
            Object Mood         = new MoodAnalyser();
            var    moodAnalyser = Mood.GetType();
            var    moodFactory  = moodAnalyserFactory.GetType();

            if (moodFactory.Equals(moodAnalyser))
            {
                return(true);
            }
            return(false);
        }