Return to Snippet

Revision: 41351
at February 16, 2011 23:40 by vriesdewie


Initial Code
private static Object GetClassInstance(Assembly assembly, String className, Object args)
    {
      
      Object result = null;
      if (assembly == null || String.IsNullOrEmpty(className)) return null;

      try
      {
        foreach (Type type in assembly.GetTypes())
        {
          if (type.IsClass && type.FullName.EndsWith("." + className))
          {
            if (args == null)
            {
              // Use the raw null value for the args parameter. A parameter
              // of type Object with a value of null causes an exception.
              // Also an empty list of parameters like Object args[] = new {};
              // causes an exception.
              result = Activator.CreateInstance(type, null);
            }
            else result = Activator.CreateInstance(type, args);
            break;
          }
        }
      }
      catch
      {
        result = null;
      }
      return result;
    }

    private static String GetQuery(EntitySpaces.Interfaces.esDynamicQuery qry)
    {

      Assembly assembly;
      esDataRequest r = new esDataRequest();
      Object[] args = new Object[] { r };
      String sql = String.Empty;
      String className = "QueryBuilder";

      try
      {
        qry.GetType().InvokeMember("PopulateRequest", BindingFlags.NonPublic |
         BindingFlags.InvokeMethod | BindingFlags.Instance, null, qry, args);
        String assemblyName = qry.es.Connection.ProviderSignature.DataProviderName;
        args = new Object[] { qry, new SqlCommand(), 1 };
        assembly = Assembly.Load(assemblyName);
        Object qb = GetClassInstance(assembly, className, null);
        sql = qb.GetType().InvokeMember("BuildQuery", BindingFlags.NonPublic |
          BindingFlags.Static | BindingFlags.InvokeMethod, null, qb, args).ToString();
      }
      catch
      {
        sql = null;
      }
      return sql;
    }

Initial URL

                                

Initial Description
After having constructed a query, this piece of code can obtain the SQL statement before executing the query.

Initial Title
Get SQL of Dynamic Query in EntitySpaces

Initial Tags

                                

Initial Language
C#