Пример #1
0
 public static object CreateClass(string ClassToCall)
 {
     // --------------------------------------------------------------
     // Call a static/shared method of a class to perform the
     // necessary action.
     // --------------------------------------------------------------
     try
     {
         int PosPeriod = ClassToCall.IndexOf(".");
         if (PosPeriod == -1)
         {
             throw new Exception("No namespace specified in action: " + ClassToCall);
         }
         Type t = null;
         foreach (Assembly Assemb in AppDomain.CurrentDomain.GetAssemblies())
         {
             t = Assemb.GetType(ClassToCall, false, true);
             if (t != null)
             {
                 break;
             }
         }
         if ((t == null))
         {
             throw new Exception("Cannot find type: " + ClassToCall);
         }
         return(Activator.CreateInstance(t));
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     return(null);
 }
Пример #2
0
        public static object CallMethodOfClass(string ClassToCall, string MethodToCall, List <object> Arguments)
        {
            // --------------------------------------------------------------
            // Call a static/shared method of a class with the specified
            // arguments.
            // --------------------------------------------------------------
            try
            {
                int PosPeriod = ClassToCall.IndexOf(".");
                if (PosPeriod == -1)
                {
                    throw new Exception("No namespace specified in action: " + ClassToCall);
                }

                string AssemblyNameToFind = ClassToCall.Substring(0, PosPeriod);
                Type   t = null;
                foreach (Assembly Assemb in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (AssemblyNameToFind == Assemb.GetName().Name)
                    {
                        t = Assemb.GetType(ClassToCall, true, true);
                    }
                }
                if (t == null)
                {
                    throw new Exception("Cannot find type: " + ClassToCall);
                }

                MethodInfo Method = t.GetMethod(MethodToCall);
                if (Method == null)
                {
                    throw new Exception("Cannot find method '" + MethodToCall + "' in class '" + ClassToCall + "'");
                }

                object[] Params = new object[Arguments.Count];
                Arguments.CopyTo(Params);
                return(Method.Invoke(null, Params));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message);
            }
        }