locked
Dynamic Code Compile/Execution in Custom Control RRS feed

  • Question

  • User1454272008 posted

    I am building a custom control.  Part of the UI for this custom control is an XML defined data entry form.  The fields, layout, etc... are defined in the XML.  I would like to allow for certain properties, such as default values for the controls specified in the xml, to be code expressions.

    A specific Example for use on an Intranet: i want to be able to write something like

    <Field ...other properties... DefaultValueExpression="System.Web.HttpContext.Current.Request.UserHostName" />

    And then, when I build the control assign that as an Expression to the Text property of the field (a textbox control).  I know I can build a CodeSnippetExpression to represent it.  Basically new CodeSnippetExpression ("System.Web.HttpContext.Current.Request.UserHostName"), but how do i then, from code and not declaritively, assign this expression to the Text property of a dynamically created textbox control?

    Any help with this would be great.

    Thanks,
      Peter

    Friday, September 25, 2009 1:05 PM

Answers

All replies

  • User191633014 posted

    have a good time friend:

    http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 25, 2009 3:08 PM
  • User1454272008 posted

    That is one of the ways I am considering already, and I appreciate the link as it has a good working example i can use.  But I am looking for an approach that works kind of the same way that expressions work.  Kind of like the Code Expression (http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx), but that can be used in code-behind/custom control.

    I kind of assumed that if I Can do it in design view i should be able to do it in code.

     

    Friday, September 25, 2009 4:11 PM
  • User191633014 posted

    Code Expression (http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx), but that can be used in code-behind/custom control.
     

    in my opinion this is quite different think

    Friday, September 25, 2009 4:23 PM
  • User-1020774567 posted

    using System;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;

    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ICodeCompiler objCodeCompiler = new CSharpCodeProvider().CreateCompiler();
            CompilerParameters objCompilerParameters = new CompilerParameters();

            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.ReferencedAssemblies.Add("System.Web.dll");
            
            objCompilerParameters.GenerateInMemory = true;

            string strCode = "";
            strCode += "\t\tusing System;";
            strCode += "\t\tusing System.IO;";
            strCode += "\t\tusing System.Web;";

            strCode += "\t\tnamespace NS";
            strCode += "\t\t{";
            strCode += "\t\t\tpublic class DynamicCode";
            strCode += "\t\t\t{";

            strCode += "\t\t\t\tpublic void ExecuteCode()";
            strCode += "\t\t\t\t{";
            strCode += "\t\t\t\t\tHttpContext.Current.Response.Write(\"Hello World\");";
            strCode += "\t\t\t\t}";
            strCode += "\t\t\t}";

            strCode += "}";

            CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode);
            if (objCompileResults.Errors.HasErrors == true)
            {
                Response.Write("Error: Line>" + objCompileResults.Errors[0].Line.ToString() + ", " + objCompileResults.Errors[0].ErrorText);
                return;
            }

            System.Reflection.Assembly objAssembly = objCompileResults.CompiledAssembly;
            Object objTheClass = objAssembly.CreateInstance("NS.DynamicCode");
            if (objTheClass == null)
            {
                Response.Write("Can't load class..... ");
                return;
            }
            
            try
            {
                //object objResult = objTheClass.GetType().InvokeMember("ExecuteCode", BindingFlags.InvokeMethod, null, objTheClass, objFunctionParameters);
                object objResult = objTheClass.GetType().InvokeMember("ExecuteCode", BindingFlags.InvokeMethod, null, objTheClass, null);

                Response.Write(objResult);
            }
            catch (Exception ex)
            {
                Response.Write("Error:" + ex.Message);
            }
        }
    }

    Monday, October 19, 2009 7:30 AM